Dar*_*ana 5 ios swift promisekit swift3
我在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)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
它在第二行给出以下错误:"无法在当前上下文中推断闭包类型"
错误代码行:
Promise<Double> { completion, reject -> Void in
Run Code Online (Sandbox Code Playgroud)
我无法确定它为什么会出现此错误.有没有快速的专家可以帮助我.
谢谢!
Kam*_*ran 10
在当前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)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2527 次 |
最近记录: |