Does Swift support reflection?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Swift does support reflection, but in a deliberately limited way. The language offers runtime inspection tools such as Mirror and type(of:), yet it does not aim to provide the fully dynamic reflection model you might expect from languages such as C# or Java.
That design is intentional. Swift emphasizes static typing, performance, and predictable APIs, so reflection exists mainly for inspection, debugging, and certain framework-style utilities rather than for unrestricted runtime metaprogramming.
What Swift Reflection Can Do
The standard tool is Mirror. It lets you inspect an instance, enumerate its stored properties, and look at labels and child values.
This is useful for diagnostics, generic logging, quick admin tooling, and some lightweight serialization helpers. You can inspect structure at runtime without hardcoding every field name.
Swift also provides type(of:) when all you need is the dynamic type:
What Swift Reflection Cannot Do Easily
Swift reflection is intentionally narrower than reflection in highly dynamic languages. In normal Swift code, you cannot:
- invoke arbitrary methods by name using built-in reflection,
- modify stored properties dynamically by string key in a general-purpose way,
- inspect everything about a type with the same depth available in some other runtimes.
That is why many Swift solutions that look like "reflection problems" are better solved with protocols, generics, Codable, or key paths instead.
For example, if you want structured serialization, Codable is usually the right tool. If you want configurable property access, typed key paths are often safer and clearer than reflection.
Customizing Reflection Output
Swift also lets a type customize how it appears to reflection clients by conforming to CustomReflectable.
This is especially helpful when you want debug output that hides sensitive fields or presents a simpler view than the raw stored properties.
Reflection Versus Better Swift Tools
Because Swift is strongly typed, reflection is not the first tool you should reach for. Consider these alternatives first:
- use protocols when behavior differs by type,
- use generics when the logic is compile-time generic,
- use key paths for safe property access,
- use
Codablefor encoding and decoding, - use manual mapping when public API clarity matters.
Reflection is best when you are inspecting unknown values, building debug utilities, or writing framework code that must handle many model types without knowing them in advance.
Common Pitfalls
- Expecting Java-style or C#-style dynamic reflection. Swift does less at runtime by design.
- Using
Mirrorin performance-critical code paths. It is convenient, not free. - Trying to use reflection where
Codable, protocols, or key paths would produce clearer code. - Assuming reflection exposes every detail of a type the way a compiler or debugger does.
- Forgetting that reflection-based code is usually more fragile than explicit typed code.
Summary
- Swift supports reflection, mainly through
Mirrorandtype(of:). - Reflection in Swift is useful for inspection, logging, and some framework utilities.
- It is intentionally more limited than reflection in many dynamic or runtime-heavy languages.
- '
CustomReflectablelets a type control how it appears during reflection.' - For most production features, Swift-native tools such as protocols, key paths, and
Codableare usually better choices.
Related reading
- Does Swift support reflection?
- Download a file with Android, and showing the progress in a ProgressDialog
- Download a file with Android, and showing the progress in a ProgressDialog
- Download Xcode simulator directly
- Draw dotted not dashed line, with IBDesignable in 2017
- Draw line in UIView
- Draw text along circular path in Swift for iOS
- Drawing UIBezierPath on code generated UIView
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.