![](/img/trans.png)
[英]Contextual closure type '(Any?) -> Void' expects 1 argument, but 2 were used in closure body
[英]Contextual closure type … expects 2 arguments, but 3 were used in closure body
親愛的高級程序員,
您能否協助一個相當新的程序員編輯此代碼。 似乎新版本的 Xcode 不支持以下代碼並顯示錯誤:
**"Contextual closure type '(Directions.Session, Result<RouteResponse, DirectionsError>) -> Void' (aka '((options: DirectionsOptions, credentials: DirectionsCredentials), Result<RouteResponse, DirectionsError>) -> ()') expects 2 arguments, but 3 were used in closure body"**
代碼直接從 mapbox 文檔網站復制而來。 任何形式的幫助將不勝感激。 提前致謝。
func getRoute(from origin: CLLocationCoordinate2D,
to destination: MGLPointFeature) -> [CLLocationCoordinate2D]{
var routeCoordinates : [CLLocationCoordinate2D] = []
let originWaypoint = Waypoint(coordinate: origin)
let destinationWaypoint = Waypoint(coordinate: destination.coordinate)
let options = RouteOptions(waypoints: [originWaypoint, destinationWaypoint], profileIdentifier: .automobileAvoidingTraffic)
_ = Directions.shared.calculate(options) { (waypoints, routes, error) in
guard error == nil else {
print("Error calculating directions: \(error!)")
return
}
guard let route = routes?.first else { return }
routeCoordinates = route.coordinates!
self.featuresWithRoute[self.getKeyForFeature(feature: destination)] = (destination, routeCoordinates)
}
return routeCoordinates
}
請開發人員學習閱讀錯誤消息和/或文檔。
錯誤清楚地表明閉包的類型是(Directions.Session, Result<RouteResponse, DirectionsError>) -> Void
(2 個參數),它代表一個 session (一個元組) 和一個包含響應和潛在錯誤的自定義Result
類型.
你必須寫一些像
_ = Directions.shared.calculate(options) { (session, result) in
switch result {
case .failure(let error): print(error)
case .success(let response):
guard let route = response.routes?.first else { return }
routeCoordinates = route.coordinates!
self.featuresWithRoute[self.getKeyForFeature(feature: destination)] = (destination, routeCoordinates)
}
}
除了這個問題之外,不可能從異步任務中返回某些東西,例如,您必須添加一個完成處理程序
func getRoute(from origin: CLLocationCoordinate2D,
to destination: MGLPointFeature,
completion: @escaping ([CLLocationCoordinate2D]) -> Void) {
並在閉包內的success
案例結束時調用completion(route.coordinates!)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.