properties - How to declare in Swift a function parameter that is a property name -
i want have class can access specified property in class @ runtime. guessing possible if pass closure or function in parameter. example:
class x { let pointfunction : (thing) -> (cgpoint) init(pointfunction : (thing) -> (cgpoint)) { self.pointfunction = pointfunction } func action(#thing : thing) { var p = pointfunction(thing) ... } }
and
class func getposition(thing : thing) -> cgpoint { return thing.whereami }
then pass getposition
when creating x.
but there syntax can pass in whereami
function name? then, in action
method can do:
thing.pointfunction
you need thing->cgpoint
. function literal function there:
let x = x(pointfunction: {$0.whereami})
if promise whereami
method rather property, other tricks (see http://oleb.net/blog/2014/07/swift-instance-methods-curried-functions/), works methods. inexplicably imo swift properties not methods. if were, pass thing.whereami
.
that said, you'd have change init take thing -> () -> cgpoint
, little more awkward anyway, or you'd have have 2 inits this:
init(pointfunction : (thing) -> (cgpoint)) { self.pointfunction = pointfunction } convenience init(pointmethod : thing -> () -> cgpoint) { self.init(pointfunction: {pointmethod($0)()}) }
so really, {$0.whereami}
approach easier work in cases curried approach, , i'd recommend here.
Comments
Post a Comment