2
votes

I'm currently trying to convert my Swift iOS app to Swift 2. I've removed 99% of compiler errors but this one remains:

Cannot convert value of type '(CMAltitudeData!, NSError!) -> Void' to expected argument type 'CMAltitudeHandler' (aka '(Optional, Optional) -> ()')

This is in response to this function:

func startAltimeterUpdate() {
    self.altimeter.startRelativeAltitudeUpdatesToQueue(NSOperationQueue.currentQueue()!,
        withHandler: { (altdata:CMAltitudeData!, error:NSError!) -> Void in
            self.handleNewMeasure(pressureData: altdata)
    })
}

I am having a hard time understanding this error... what on earth is Xcode trying to tell me here?

2

2 Answers

1
votes

You are trying to force an optional to become non-optional. Swift doesn't like that. Instead, try this:

func startAltimeterUpdate() {
    self.altimeter.startRelativeAltitudeUpdatesToQueue(NSOperationQueue.currentQueue()!,
        withHandler: { (altdata, error) -> Void in
            if let data = altdata {
                self.handleNewMeasure(pressureData: data)
            } else {
                // altdata is nil
            }
    })
}
2
votes

It's quite easy.

-click on startRelativeAltitudeUpdatesToQueue to get the documentation of the symbol.

You will see that the CMAltitudeHandler handler is declared as

CMAltitudeHandler = (CMAltitudeData?, NSError?) -> Void

Both parameters are optional ? rather than implicit unwrapped optional !

That's what the error message says.