Swift type issue

Thanks for the SwiftStarter App!

I have issues converting the 'Notify on Orientation Change' Objective-C code to Swift code for the BMI160.

Objective-C (from API documentation):

[accelerometerBMI160.orientationEvent startNotificationsWithHandlerAsync:^(MBLOrientationData *obj, NSError *error) {
    NSLog(@Flipped Me: %@", obj);
}];

My Swift code:


func startAccelerometerOrientation() {

  var accelerometerBMI160: MBLAccelerometerBMI160 = self.device.accelerometer as! MBLAccelerometerBMI160            

  accelerometerBMI160.orientationEvent.startNotificationsWithHandlerAsync({(obj: MBLOrientationData, error: NSError) -> Void in

    NSLog("Flipped Me: %@", obj)

   })

}


This results in the following error:

Cannot convert value of type '(MBLOrientationData, NSError) -> Void' to expected argument type 'MBLNotificationHandler?'

Appreciate your help.


Comments

  • Try this:
    accelerometer.orientationEvent.startNotificationsWithHandlerAsync({ (data:AnyObject?, error:NSError?) -> Void in
        if let orientation = data as? MBLOrientationData {
            print("flipped me: \(orientation)")
        }
    })
This discussion has been closed.