ios - Core Motion Swift Closure issue -
i'm trying convert old game application built in obj-c new swift code. i'm having issues understanding swift closures , how use them example in the"startaccelerometerupdatestoqueue" method.
i have initialized motion manager in way
motionmanager!.accelerometerupdateinterval = (1/40)
then in viewdidload of view controller
var queue:nsoperationqueue motionmanager?.startaccelerometerupdatestoqueue(queue, withhandler: {(accelerometerdata : cmaccelerometerdata, error : nserror) in })
the "startaccelerometerupdatestoqueue" giving me error , i'm pretty sure didn't understand correct closure syntax.
any ideas?
actually, got signature wrong – arguments closure need optionals (since passed objective-c, nil
). because of that, arguments provide don't match existing method signature, , because of error.
take @ ios 8 api docs, provide swift signatures:
func startaccelerometerupdatestoqueue(_ queue: nsoperationqueue!, withhandler handler: cmaccelerometerhandler!)
and cmaccelerometerhandler
defined as
typealias cmaccelerometerhandler = (cmaccelerometerdata!, nserror!) -> void
thus, call should be:
motionmanager?.startaccelerometerupdatestoqueue(queue, withhandler: {(accelerometerdata : cmaccelerometerdata!, error : nserror!) in })
and function/method takes closure it's last argument, can leave out of argument list , write after call (trailing closure syntax – example leaves out types, can inferred, optional):
motionmanager?.startaccelerometerupdatestoqueue(queue) { accelerometerdata, error in }
Comments
Post a Comment