Swift
reflection
programming
iOS development
Swift language

Does Swift support reflection?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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.

swift
1import Foundation
2
3struct User {
4    let id: Int
5    let name: String
6    let isAdmin: Bool
7}
8
9let user = User(id: 42, name: "Maya", isAdmin: true)
10let mirror = Mirror(reflecting: user)
11
12print("Type:", mirror.subjectType)
13
14for child in mirror.children {
15    print(child.label ?? "unknown", child.value)
16}

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:

swift
let value: Any = user
print(type(of: value))

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.

swift
1import Foundation
2
3struct Credentials: CustomReflectable {
4    let username: String
5    let password: String
6
7    var customMirror: Mirror {
8        Mirror(self, children: [
9            "username": username,
10            "password": "REDACTED"
11        ])
12    }
13}
14
15let creds = Credentials(username: "mark", password: "secret")
16let mirror = Mirror(reflecting: creds)
17
18for child in mirror.children {
19    print(child.label ?? "unknown", child.value)
20}

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 Codable for 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 Mirror in 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 Mirror and type(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.
  • 'CustomReflectable lets a type control how it appears during reflection.'
  • For most production features, Swift-native tools such as protocols, key paths, and Codable are usually better choices.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.