Cannot convert value of type error in Swift

1.7k views Asked by At

I'm pretty new to Swift, so I'm following a tutorial on how to make a pedometer app from this page: http://shrikar.com/ios-swift-development-step-counter-app-using-pedometer-data/

However, that was from a while ago, so I've been working on updating it and not getting very far. I keep getting this error:

Cannot convert value of type '(CMPedometerData?, NSError?) -> Void' to expected argument type 'CMPedometerHandler' (aka '(Optional<CMPedometerData>, Optional<Error>) -> ()')

Here's my code so far:

if(CMPedometer.isStepCountingAvailable()){
        let fromDate = NSDate(timeIntervalSinceNow: -86400 * 7)
        self.pedoMeter.queryPedometerDataFromDate(fromDate as Date, toDate: NSDate() as Date) { (data : CMPedometerData?, error: NSError?) -> Void in
            DispatchQueue.main.async(){
                if(error == nil){
                    self.steps.text = "\(data.numberOfSteps)"
                }
            }
        }

        self.pedoMeter.startPedometerUpdatesFromDate(newDay) { (data: CMPedometerData?, error: NSError?) -> Void in
            DispatchQueue.main.async(){
                if(error == nil){
                    self.steps.text = "\(data.numberOfSteps)"
                }
            }
        }
    }

The error occurs in the following line:

{ (data : CMPedometerData?, error: NSError?) -> Void in
            DispatchQueue.main.async(){

Any help appreciated.

1

There are 1 answers

1
Keiwan On BEST ANSWER

It expects Error? whereas you have used NSError?. Try

(data : CMPedometerData?, error: Error?) -> Void