Search code examples
iosswiftswift3promisekit

Swift 3.0: Unable to infer closure type in the current context ,PromiseKit


I have following code in swift 3.0, Where I am using PromiseKit.

func calculateTravelTime(from: CLLocation, to: CLLocation) -> Promise<Double> {
     Promise<Double> { completion, reject -> Void in
        let request = MKDirections.Request()
        request.transportType = .walking

        let fromPlacemark = MKPlacemark(coordinate: from.coordinate)
        let toPlacemark = MKPlacemark(coordinate: to.coordinate)

        let fromMapPoint = MKMapItem(placemark: fromPlacemark)
        let toMapPoint = MKMapItem(placemark: toPlacemark)

        request.source = fromMapPoint
        request.destination = toMapPoint
        request.requestsAlternateRoutes = false

        let directions = MKDirections(request: request)
        directions.calculate { response, error in
            if error != nil {
                reject(error!)
            } else {
                let time = (response?.routes.first?.expectedTravelTime ?? 0) / 60.0
               completion(time)
            }
        }

    }
} 

It's giving the following error on the second line : "Unable to infer closure type in the current context"

error code line :

Promise<Double> { completion, reject -> Void in

I am not able to identify why it's giving this error. Is there any swift expert who can help me this.

Thank you!


Solution

  • In the current PromiseKit version this

    Promise<T> { fulfill, reject -> Void in }

    is changed to

    Promise<T> { seal -> Void in }

    So, your new implementation will change to this,

    func calculateTravelTime(from: CLLocation, to: CLLocation) -> Promise<Double> {
         Promise<Double> { seal -> Void in
            let request = MKDirections.Request()
            request.transportType = .walking
    
            let fromPlacemark = MKPlacemark(coordinate: from.coordinate)
            let toPlacemark = MKPlacemark(coordinate: to.coordinate)
    
            let fromMapPoint = MKMapItem(placemark: fromPlacemark)
            let toMapPoint = MKMapItem(placemark: toPlacemark)
    
            request.source = fromMapPoint
            request.destination = toMapPoint
            request.requestsAlternateRoutes = false
    
            let directions = MKDirections(request: request)
            directions.calculate { response, error in
                if error != nil {
                    seal.reject(error!)
                } else {
                    let time = (response?.routes.first?.expectedTravelTime ?? 0) / 60.0
                   seal.fulfill(time)
                }
            }
    
        }
    }