我在 Swift 3.0 中使用 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)
}
}
}
}
第二行出现以下错误:
“无法推断当前上下文中的闭包类型”
错误代码行:
Promise<Double> { completion, reject -> Void in
我无法确定为什么会出现这个错误。是否有任何熟悉Swift的专家可以帮助我解决这个问题。
谢谢!
Promise<Double> { ...
以外的所有内容都注释掉,以便隔离错误吗? 此外,在文档中提到,你的类型应该是Thenable
和CatchMixin
:https://github.com/mxcl/PromiseKit/blob/master/Sources/Promise.swift#L8 也许是因为你的 Double 没有实现这些协议。 - rgkobashicompletion, reject -> Void
作为参数)在新版本中不存在,我猜应该有一个相当的方法。我快速查看了文档,没有看到这种类型的init。 - rgkobashi