I notice properties that aren't defined anywhere in the Swift global definition, such as dynamicType and Type in Any.Type. How do I find out the properties that one can access but aren't documented? Is there a way to do introspection in Swift?
Asked
Active
Viewed 931 times
1 Answers
3
Swift has read only reflection at this point. It's not that great yet, but here's an example of what's available:
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")
}

kmikael
- 4,962
- 3
- 32
- 34
-
Right now this gives an error in both the playground and the command line: data =Assertion failed: (ty->isLegalSILType() && "constructing SILType with type that should have been " "eliminated by SIL lowering"), function SILType, file /SourceCache/lldb_KLONDIKE/lldb_KLONDIKE-320.3.103/llvm/tools/swift/include/swift/SIL/SILType.h, line 73. Abort trap: 6 – David Berry Jun 22 '14 at 17:11
-
It works for me when I make an Xcode project. I'm sorry I forgot to mention that. – kmikael Jun 22 '14 at 17:20