Last active
August 29, 2015 14:24
-
-
Save cwagdev/a7b1d38f80612f21dcd4 to your computer and use it in GitHub Desktop.
This file contains 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
protocol ValueForKeyLookupable { | |
func valueForKey(key: String) -> Any? | |
} | |
extension ValueForKeyLookupable { | |
func valueForKey(key: String) -> Any? { | |
let mirror = reflect(self) | |
for index in 0..<mirror.count { | |
let child = mirror[index] | |
if key == child.0 {return child.1.value} | |
} | |
return nil | |
} | |
} | |
struct Pt: ValueForKeyLookupable { | |
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) | |
p.valueForKey("x") | |
p.valueForKey("y") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment