Swift 3.0:无法推断当前上下文中的闭包类型,PromiseKit

时间:2018-12-13 05:36:00

标签: ios swift swift3 promisekit

我在使用PromiseKit的Swift 3.0中有以下代码。

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)
            }
        }

    }
} 

第二行给出以下错误: “无法在当前上下文中推断闭包类型”

错误代码行:

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

我无法确定为什么会出现此错误。有没有能帮助我的迅速专家。

谢谢!

1 个答案:

答案 0 :(得分:8)

在当前的PromiseKit版本中

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

已更改为

Promise<T> { seal -> Void in }

因此,您的新实现将更改为此,

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)
            }
        }

    }
}