Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@onevcat 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

This comment has been minimized.

Copy link

@onevcat 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

This comment has been minimized.

Copy link

@abhishekmunie abhishekmunie commented Oct 27, 2014

infix operator --> {}

@wajatimur

This comment has been minimized.

Copy link

@wajatimur wajatimur commented Nov 14, 2014

+1

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