iOS Swift: Mapbox 路线计算不工作,"Value of type 'Result<RouteResponse, DirectionsError>' has no member 'first'"

iOS Swift: Mapbox route calculation not working, "Value of type 'Result<RouteResponse, DirectionsError>' has no member 'first'"

所以我正在按照我在 YT 上找到的教程“构建一个 iOS 应用程序:(2/4) 使用 Mapbox Navigation SDK 计算路线”并在尝试自己进行操作时遇到了问题。

不知何故 xCode 在创建 calculateRoute 函数时抛出多个错误,尤其是在 Directions.shared.calculate(..) 中。这是我的代码:

import UIKit
import Mapbox
import MapboxNavigation
import MapboxDirections
import MapboxCoreNavigation

class ThirdViewController: UIViewController, MGLMapViewDelegate{

//is the View that shows the map
var mapView: NavigationMapView!

//Saves the route
var directionsRoute: Route?

//creates the button
var navigateButton: UIButton!



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    //view.backgroundColor = UIColor(red: 0.19, green: 0.21, blue: 0.24, alpha: 1.00)
    mapView = NavigationMapView(frame: view.bounds)
    mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    view.addSubview(mapView)
    
    mapView.delegate = self
    mapView.showsUserLocation = true
    mapView.setUserTrackingMode(.follow, animated: true)
    
    addButton()
    
}

func addButton(){
    navigateButton = UIButton(frame: CGRect(x: (view.frame.width/2) - 100, y: view.frame.height - 200, width: 200, height: 50))
    navigateButton.backgroundColor = UIColor.white
    navigateButton.setTitle("Route finden", for: .normal)
    navigateButton.setTitleColor(UIColor(red: 59/255, green: 178/255, blue: 208/255, alpha: 1), for: .normal)
    navigateButton.titleLabel?.font = UIFont(name: "AvenirNext-Demibold", size: 18)
    navigateButton.layer.cornerRadius = 25
    navigateButton.layer.shadowOffset = CGSize(width: 0, height: 10)
    //navigateButton.layer.shadowColor = C
    navigateButton.layer.shadowRadius = 5
    navigateButton.layer.shadowOpacity = 0.3
    navigateButton.addTarget(self, action: #selector(navigateButtonWasPressed(_:)), for: .touchUpInside)
    view.addSubview(navigateButton)
}

@objc func navigateButtonWasPressed(_ sender: UIButton){
    
}

func calculateRoute(from originCoor: CLLocationCoordinate2D, to destinationCoor: CLLocationCoordinate2D, completion: @escaping (Route?, Error?) -> Void ){
    let origin = Waypoint(coordinate: originCoor, coordinateAccuracy: -1, name: "Start")
    let destination = Waypoint(coordinate: destinationCoor, coordinateAccuracy: -1, name: "Finish")
    
    let options = NavigationRouteOptions(waypoints: [origin, destination], profileIdentifier: .automobileAvoidingTraffic)
    
    _ = Directions.shared.calculate(options, completionHandler: { (wayponts, routes, error) in
        self.directionsRoute = routes?.first //HERE IS THE ERROR
        
        let coordinateBounds = MGLCoordinateBounds(sw: destinationCoor, ne: originCoor)
    })
}

}

我已经标记了导致问题的行。 xCode 告诉抛出以下错误:

  1. 上下文闭包类型'(Directions.Session, Result) -> Void'(又名'((options: DirectionsOptions, credentials: DirectionsCredentials), Result) -> ()') 需要 2 个参数,但在闭包体中使用了 3 个 一旦我从上面的 lign 中删除第三个参数(错误),就会出现两个新错误: 2)'Result<RouteResponse, DirectionsError>'类型的值没有成员'first' 3) 不能对 'Result<RouteResponse, DirectionsError>'
  2. 类型的非可选值使用可选链接

我是初学者,似乎无法修复它。我也找不到任何关于它的文档。 该行代码应将 directionsRoute 设置为返回路线中的第一条路线。我一步一步跟着教程,我错过了什么吗? 谢谢!

第二个参数——只有两个——是一个Result类型,一个具有关联值的枚举。它包含 routeserror

你必须写

_ = Directions.shared.calculate(options, completionHandler: { (waypoints, result) in
    switch result {
        case .success(let response):
            guard let route = response.routes?.first else { return }
            self.directionsRoute = route
            let coordinateBounds = MGLCoordinateBounds(sw: destinationCoor, ne: originCoor)
        case .failure(let error): print(error)
    }  
})