我注意到Swift全局定义中未定义的属性,例如DynamicType和Any.Type中的Type。如何找到可以访问但未记录的属性?有没有办法在Swift中进行内省?
答案 0 :(得分:3)
struct Bookmark {
let title: String, url: String
}
let bookmark = Bookmark(title: "Stack Overflow", url: "http://stackoverflow.com")
var mirror = reflect(bookmark)
for var propertyNumber = 0; propertyNumber < mirror.count; ++propertyNumber {
let (propertyName, propertyMirror) = mirror[propertyNumber]
println("\(propertyName) = \(propertyMirror.summary), \(propertyMirror.count) children")
}