Last active
February 11, 2016 23:31
-
-
Save peeblesjs/9288f79322ed3119ece4 to your computer and use it in GitHub Desktop.
A naive "valueForKey:" operator in Swift
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
operator infix --> {} | |
func --> (instance: Any, key: String) -> Any? { | |
let mirror = reflect(instance) | |
for index in 0 ..< mirror.count { | |
let (childKey, childMirror) = mirror[index] | |
if childKey == key { | |
return childMirror.value | |
} | |
} | |
return nil | |
} | |
//Example | |
struct MyPoint { | |
let x: Float | |
let y: Float | |
} | |
let point = MyPoint(x: 1, y: 2) | |
point --> "x" | |
point --> "y" |
It seems that was a issue in Playground Xcode 6 beta 6. When using it in project, everything goes well.
infix operator --> {}
+1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems not valid anymore in current beta (Xcode 6 beta 6), due to the
childKey
always returning an empty value. Could you confirm it too? THX