Swift 3.0: 无法推断当前上下文中的闭包类型,PromiseKit
Swift 3.0: Unable to infer closure type in the current context ,PromiseKit
我在 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)
}
}
}
}
它在第二行给出了以下错误:
"Unable to infer closure type in the current context"
错误代码行:
Promise<Double> { completion, reject -> Void in
我无法确定为什么会出现此错误。有 swift 专家可以帮助我吗?
谢谢!
在当前 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)
}
}
}
}
我在 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)
}
}
}
}
它在第二行给出了以下错误: "Unable to infer closure type in the current context"
错误代码行:
Promise<Double> { completion, reject -> Void in
我无法确定为什么会出现此错误。有 swift 专家可以帮助我吗?
谢谢!
在当前 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)
}
}
}
}