Skip to content

Instantly share code, notes, and snippets.

@huandu
Created November 22, 2014 18:00
Show Gist options
  • Select an option

  • Save huandu/d976f7994d2fd159398b to your computer and use it in GitHub Desktop.

Select an option

Save huandu/d976f7994d2fd159398b to your computer and use it in GitHub Desktop.
List properties of a swift class.
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