# Mirror

Mirror 是 Swift 提供的一个用于反射的类，我们介绍一下 他的简单用法。

```swift
struct Point {
    let x: Int
    let y: Int
    let z: Int
}

let p = Point(x: 1, y: 2, z: 3)
let mirror = Mirror(reflecting: p)
for child in mirror.children {
    print("label:\(child.label)")
    print("value:\(child.value)")
}

// 输出
// label:Optional("x")
// value:1
// label:Optional("y")
// value:2
// label:Optional("z")
// value:3
```

Mirror 可以轻松将一个对象的属性和值反射出来。

但是 Swift 由于弱化了反射的功能，它无法赋值，你可以读，但是你不能写，即使写，也不是原来的那个对象了，因为它会给你个 copy 版本。 这也就是为什么 SwiftJSON 的库要用操作内存偏移来给对象赋值，因为没办法通过反射赋值。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yongpenglovemimi123.gitbook.io/henry/swift/mirror.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
