> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/swift/debugging/mirror.md).

# Mirror

{% hint style="info" %}
**Reflection** in Swift allows us to use the [Mirror](https://developer.apple.com/documentation/swift/mirror) API to **inspect** and manipulate <mark style="color:red;">**arbitrary values**</mark> at <mark style="color:orange;">**runtime**</mark>. 👉 [SwiftLee](https://www.avanderlee.com/swift/reflection-how-mirror-works/)
{% endhint %}

{% tabs %}
{% tab title="⬇️ 應用" %}
💾 程式：[paiza.io](https://paiza.io/projects/BgeXldbalSZcvt3C_5qcQg)

* [HasMirrors](/ios/swift/debugging/hasmirrors.md) - use **Mirror** to inspect types.
* [CaseReflectable](/ios/swift/debugging/casereflectable.md) - inspect [enum](/ios/swift/type/category/basic/enum.md) case.
* [NonNominalTypeWrapper](/ios/swift/debugging/nonnominaltypewrapper.md) - help inspect [non-nominal types](/ios/swift/type/category/non-nominal-types.md).
* [enum case pattern](/ios/swift/pattern-matching/enum-case-pattern.md) - use Mirror to pattern match an enum case.
  {% endtab %}

{% tab title="🔸 定義" %}

```swift
// --------------------
//     Type aliases
// --------------------

// An element of the reflected instance’s structure
// label: nil | stored property name | enum case name.
typealias Mirror.Child = (label: String?, value: Any)

// The type used to represent substructure.
typealias Mirror.Children = AnyCollection<Mirror.Child>

// -------------
//     Types
// -------------

// Playgrounds and debugger will use this as suggestion to show subject.
enum Mirror.DisplayStyle {
    case `class`
    case collection
    case dictionary
    case `enum`
    case optional
    case set
    case `struct`
    case tuple
}
```

{% endtab %}

{% tab title="📗 參考" %}

* [x] SwiftLee ⟩ [Reflection in Swift: How Mirror works](https://www.avanderlee.com/swift/reflection-how-mirror-works/)
* [ ] AppVenture.me ⟩ [The Swift Reflection API and what you can do with it](https://appventure.me/guides/swift_reflection/introduction.html)
* [ ] Sundell ⟩ [Reflection in Swift](https://www.swiftbysundell.com/articles/reflection-in-swift/)
* [ ] NSHipster ⟩ [Mirror / Custom​Reflectable / Custom​Leaf​Reflectable](https://nshipster.com/mirror/)
  {% endtab %}

{% tab title="📘 手冊" %}

* [Swift](https://developer.apple.com/documentation/swift) ⟩&#x20;
  * [Debugging and Reflection](https://developer.apple.com/documentation/swift/swift_standard_library/debugging_and_reflection) ⟩ [Mirror](https://developer.apple.com/documentation/swift/mirror) (<mark style="color:red;">struct</mark>)
    * [Mirror.Child](https://developer.apple.com/documentation/swift/mirror/child)
    * [Mirror.Children](https://developer.apple.com/documentation/swift/mirror/children)
    * [Mirror.DisplayStyle](https://developer.apple.com/documentation/swift/mirror/displaystyle)
  * [String](https://developer.apple.com/documentation/swift/string) ⟩&#x20;
    * [.init(reflecting:)](https://developer.apple.com/documentation/swift/string/1541282-init) - String(reflecting: subject)\
      [CustomDebugStringConvertible](https://developer.apple.com/documentation/swift/customdebugstringconvertible) → [CustomStringConvertible](https://developer.apple.com/documentation/swift/customstringconvertible) → [TextOutputStreamable](https://developer.apple.com/documentation/swift/textoutputstreamable) → Swift standard library
    * .[init(describing:)](https://developer.apple.com/documentation/swift/string/2427941-init) - String(describing: subject)\
      [TextOutputStreamable](https://developer.apple.com/documentation/swift/textoutputstreamable) → [CustomStringConvertible](https://developer.apple.com/documentation/swift/customstringconvertible) → [CustomDebugStringConvertible](https://developer.apple.com/documentation/swift/customdebugstringconvertible) → Swift standard library
* Swift.org ⟩ [How Mirror Works](https://www.swift.org/blog/how-mirror-works/) - how Mirror works under the hood.
  {% endtab %}

{% tab title="👥 相關" %}

* [Pattern Matching](/ios/swift/pattern-matching.md) ⟩ [if/guard case let](/ios/swift/pattern-matching/sentence-patterns/if-guard-case-let.md)
* [filter cases](/ios/swift/type/category/basic/enum/filter-cases.md) - use Mirror to do [pattern matching](/ios/swift/pattern-matching.md).
* [HasMirrors](/ios/swift/debugging/hasmirrors.md) - helper protocol to mirror subjects.
* [CaseReflectable](/ios/swift/debugging/casereflectable.md) - use Mirror to get [enum](/ios/swift/type/category/basic/enum.md) case name/associated values.
* [NonNominalTypeWrapper](/ios/swift/debugging/nonnominaltypewrapper.md) - wrap [non-nominal types](/ios/swift/type/category/non-nominal-types.md) to conform to [Loggable](/ios/swift/debugging/hasmirrors.md).
  {% endtab %}
  {% endtabs %}

{% hint style="success" %}
關鍵字：meta programming, reflection
{% endhint %}
