获取实例对象对应的属性标签和值(不包括父类属性)
let mirror = Mirror.init(reflecting: person)
mirror.children.forEach { child in
if let label = child.label {
print("label \(label) value \(child.value)")
}
}
如果需要获取所有属性,需要遍历父类 mirror.superclassMirror
while let mirror = mirror.superclassMirror {
// ...
mirror.children
}
获取实例对象类型 mirror.displayStyle
public enum DisplayStyle : Sendable {
case `struct`
case `class`
case `enum`
case tuple
case optional
case collection
case dictionary
case set
}
网友评论