Search code examples
swiftxcodegenericsapple-watch

Swift 3 Migration: Generic parameter 'Element' could not be inferred


arrayOfDicts is declared as

let accelManager = CMSensorRecorder()
let motionManager = CMMotionManager()
var arrayOfDicts:[NSDictionary] = []

The function is defined as

    func startMotionManager() {
    WKInterfaceDevice.current().play(.start)
    self.arrayOfDicts = []
    if motionManager.isAccelerometerAvailable {
        motionManager.accelerometerUpdateInterval = 0.01
        let handler:CMAccelerometerHandler = {(data: CMAccelerometerData?, error: NSError?) -> Void in
            self.arrayOfDicts.append([
                "timestamp":data!.timestamp,
                "x":data!.acceleration.x,
                "y":data!.acceleration.y,
                "z":data!.acceleration.z
                ])
            //print("timestamp: ", data!.timestamp, ", x: ", data!.acceleration.x, ", y: ", data!.acceleration.y, ", z: ", data!.acceleration.z)
        }
        motionManager.startAccelerometerUpdates(to: OperationQueue(), withHandler: handler)
    }
}

The error occurs on the "let handler" line.

"Generic parameter 'Element' could not be inferred In call to function 'append'"

I have done some poking around and think it is because the complier isn't sure what type array "arrayOfDicts" is...but I could be way off. Any help and/or education on the subject will be greatly appreciated.

-Matt


Solution

  • The closure type CMAccelerometerHandler has been changed to:

    public typealias CMAccelerometerHandler = (CMAccelerometerData?, Error?) -> Swift.Void
    

    (From the generated header of "CMMotionManager.h".)

    Try changing the NSError? in that line to Error?.

    You may sometimes need to take such error messages like "cannot be inferred" as "there may be some type related errors". Send a bug report to promote Apple (or swift.org) to improve such diagnostics.