Swift objects Reflection API - 100%, no Objc runtime
To run the example project, clone the repo, and run pod install
from the Example directory first.
###Create a mirror for an instance
struct Person {
let name: String
var age: Int
}
var person = Person(name: "Jon", age: 27)
let mirror = Mirror(person)
Type properties names
mirror.names
//["name", "age"]
Type properties values
mirror.values
//["Jon", 27]
Type properties types
mirror.types
//[Swift.String, Swift.Int]
Type properties types short notation, skip bundle
mirror.typesShortName
//["String", "Int"]
Get property values by name
mirror["name"] // "Jon"
mirror["age"] // 27
Convert to dictionary with [PropertyName : PropertyValues]
mirror.toDictionary
//["age": 27, "name": "Jon"]
Mirror implements CollectionType, SequenceType.
Iterate over it's children MirrorItems
for item in mirror {
println(item)
}
//name: Swift.String = Jon
//age: Swift.Int = 27
Get children MirrorItems
let children = mirror.children //Array of MirrorItem
let firstKid = children[0]
//{name: "name", type: Swift.String, value: "Jon" }
- Swift 1.2
Mirror is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "Mirror"
Kostiantyn Koval, konstantin.koval1@gmail.com
Mirror is available under the MIT license. See the LICENSE file for more info.