如何在Swift中进行内省?

时间:2014-06-22 06:50:44

标签: swift introspection

我注意到Swift全局定义中未定义的属性,例如DynamicType和Any.Type中的Type。如何找到可以访问但未记录的属性?有没有办法在Swift中进行内省?

1 个答案:

答案 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")
}