tymac tymac - 5 months ago 40
Swift Question

Cannot convert value of type '(data: CMAccelerometerData!, err: NSError!)

Seems to be a Swift 2.3 update syntax error. Does this mean it should be unwrapped somewhere?


Cannot convert value of type '(data: CMAccelerometerData!, err: NSError!) -> Void' to expected argument type 'CMAccelerometerHandler' (aka '(Optional, Optional) -> ()')


Why is CMAccelerometerData not compatible with accelerometerUpdateHandler?

// error
motionManager.startAccelerometerUpdatesToQueue(motionHandlingQueue, withHandler: accelerometerUpdateHandler())


// snip

func accelerometerUpdateHandler() -> (data: CMAccelerometerData!, err: NSError!) -> Void {
let idleThreshold = 0.1

var log = [Double]()

var samplingCountdown: Int = 0

return { (data: CMAccelerometerData!, err: NSError!) -> Void in

let _stable = abs(data.acceleration.x) < idleThreshold && abs(data.acceleration.y) < idleThreshold && data.acceleration.z < -0.75

Answer

define your accelerometerHandler as follow:

func accelerometerHandler(data: CMAccelerometerData?, error: NSError?) -> Void {
    guard let data = data where error == nil else { return }
    let idleThreshold = 0.1
    var log = [Double]()
    var samplingCountdown: Int = 0
    let _stable = abs(data.acceleration.x) < idleThreshold && abs(data.acceleration.y) < idleThreshold && data.acceleration.z < -0.75
    // the rest of your code
}

when calling it:

motionManager.startAccelerometerUpdatesToQueue(motionHandlingQueue, withHandler: accelerometerHandler)
Comments