Created
November 22, 2014 18:00
-
-
Save huandu/d976f7994d2fd159398b to your computer and use it in GitHub Desktop.
List properties of a swift class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| struct Point { | |
| var x = 0.0 | |
| var y = 0.0 | |
| } | |
| class PropertyHub { | |
| var simpleProp = "Foo" | |
| var structProp = Point() | |
| // computed property is not visible in reflect(). | |
| var computedProp : Point { | |
| get { | |
| return Point(x: structProp.x + 100.0, y: structProp.y - 100.0) | |
| } | |
| } | |
| } | |
| // we have to create an instance before calling reflect(). | |
| var hub = PropertyHub() | |
| let mirror = reflect(hub) | |
| let count = mirror.count | |
| // this for loop will print: | |
| // | |
| // key: simpleProp | |
| // key: structProp | |
| for var index = 0; index < count; ++index { | |
| let key = mirror[index].0 | |
| // "super" refers to super class if any. | |
| if key == "super" && index == 0 { | |
| continue | |
| } | |
| print("key: ") | |
| println(key) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment