Skip to content

Instantly share code, notes, and snippets.

@peeblesjs
Last active February 11, 2016 23:31
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save peeblesjs/9288f79322ed3119ece4 to your computer and use it in GitHub Desktop.
Save peeblesjs/9288f79322ed3119ece4 to your computer and use it in GitHub Desktop.
A naive "valueForKey:" operator in Swift
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"
@onevcat
Copy link

onevcat commented Sep 1, 2014

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

@onevcat
Copy link

onevcat commented Sep 2, 2014

It seems that was a issue in Playground Xcode 6 beta 6. When using it in project, everything goes well.

@abhishekmunie
Copy link

infix operator --> {}

@wajatimur
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment