Sunday, 15 April 2012

objective c - Use SOMotionDetector with swift -



objective c - Use SOMotionDetector with swift -

i'm trying utilize somotiondetector in swift project

is there way translate objective c method swift?

- (void)motiondetector:(somotiondetector *)motiondetector locationchanged:(cllocation *)location { ... }

converting delegate methods obj-c swift similar creating standard swift functions, 1 rule: explicit parameter names.

in illustration so:

func motiondetector(motiondetector:somotiondetector locationchanged location:cllocation) { // here... }

why have locationchanged in front end of sec argument definition of location:cllocation?

it's selector signature remains motiondetector:locationchanged: while allowing method parameter name location.

let's consider next nsurlconnection delegate in obj-c:

- (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data

if convert swift without explicit parameter names, you'll see why awkward:

func connection(connection:nsurlconnection didreceivedata:nsdata) { self.mutabledata.appenddata(didreceivedata); // weird local variable name... }

now move didreceivedata parameter name alter local variable name data instead clarity:

func connection(connection:nsurlconnection didreceivedata data:nsdata) { self.mutabledata.appenddata(data); // ahhh.. much better! }

so can see, it's similar standard swift functions requires parameter names. note can non-delegate functions if you'd like!

objective-c swift

No comments:

Post a Comment