struct Pt { let x: Int let y: Int init(_ x: Int, _ y: Int) {self.x = x; self.y = y} init(){self.init(0, 0)} } let p = Pt(2, 3) func valueForKey(item : Any, key: String) -> Any? { let mirror = reflect(item) for index in 0..<mirror.count { let child = mirror[index] if key == child.0 {return child.1.value} } return nil } valueForKey(p, key: "x") // 2 valueForKey(p, key: "y") // 3 valueForKey(p, key: "z") // nil
One Comment
A quick stab with protocol extensions.
https://gist.github.com/cwagdev/a7b1d38f80612f21dcd4