从 Swift 2 转换为 Swift 3 时出错

Posted

技术标签:

【中文标题】从 Swift 2 转换为 Swift 3 时出错【英文标题】:Error in conversion from Swift 2 to Swift 3 【发布时间】:2016-09-21 09:24:16 【问题描述】:

这是我正在努力将 Swift 2 转换为 Swift 3 的第二个也是最后一个部分

旧的工作代码是

func calculateSegmentDirections(index: Int,
    time: NSTimeInterval, routes: [MKRoute]) 

    let request: MKDirectionsRequest = MKDirectionsRequest()
    request.source = locationArray[index].mapItem
    request.destination = locationArray[index+1].mapItem
    request.requestsAlternateRoutes = true
    request.transportType = .Automobile

    let directions = MKDirections(request: request)
    directions.calculateDirectionsWithCompletionHandler (
      (response: MKDirectionsResponse?, error: NSError?) in
      if let routeResponse = response?.routes 
        let quickestRouteForSegment: MKRoute =
        routeResponse.sort($0.expectedTravelTime <
          $1.expectedTravelTime)[0]

        var timeVar = time
        var routesVar = routes

        routesVar.append(quickestRouteForSegment)
        timeVar += quickestRouteForSegment.expectedTravelTime

        if index+2 < self.locationArray.count 
          self.calculateSegmentDirections(index+1, time: timeVar, routes: routesVar)
         else 
          self.showRoute(routesVar, time: timeVar)
          self.hideActivityIndicator()
        
       else if let _ = error 
        let alert = UIAlertController(title: nil,
          message: "Directions not available.", preferredStyle: .Alert)
        let okButton = UIAlertAction(title: "OK",
          style: .Cancel)  (alert) -> Void in
            self.navigationController?.popViewControllerAnimated(true)
        
        alert.addAction(okButton)
        self.presentViewController(alert, animated: true,
          completion: nil)
      
    )

转换后的代码是

func calculateSegmentDirections(index: Int,
    time: NSTimeInterval, routes: [MKRoute]) 

    let request: MKDirectionsRequest = MKDirectionsRequest()
    request.source = locationArray[index].mapItem
    request.destination = locationArray[index+1].mapItem
    request.requestsAlternateRoutes = true
    request.transportType = .Automobile

    let directions = MKDirections(request: request)
    directions.calculateDirectionsWithCompletionHandler (
      (response: MKDirectionsResponse?, error: NSError?) in
      if let routeResponse = response?.routes 
        let quickestRouteForSegment: MKRoute =
        routeResponse.sort($0.expectedTravelTime <
          $1.expectedTravelTime)[0]

        var timeVar = time
        var routesVar = routes

        routesVar.append(quickestRouteForSegment)
        timeVar += quickestRouteForSegment.expectedTravelTime

        if index+2 < self.locationArray.count 
          self.calculateSegmentDirections(index+1, time: timeVar, routes: routesVar)
         else 
          self.showRoute(routesVar, time: timeVar)
          self.hideActivityIndicator()
        
       else if let _ = error 
        let alert = UIAlertController(title: nil,
          message: "Directions not available.", preferredStyle: .Alert)
        let okButton = UIAlertAction(title: "OK",
          style: .Cancel)  (alert) -> Void in
            self.navigationController?.popViewControllerAnimated(true)
        
        alert.addAction(okButton)
        self.presentViewController(alert, animated: true,
          completion: nil)
      
    )

它在行上抛出一个错误

directions.calculateDirectionsWithCompletionHandler (

错误是

无法将类型 '(MKDirectionsResponse?, NSError?) -> ()' 的值转换为预期的参数类型 'MKDirectionsHandler'(又名 '(Optional, Optional) -> ()')

如果有人可以帮助我,我将非常感激!

【问题讨论】:

那些函数太长了。您应该在转换之前先重构它们。然后它将使它们更容易转换。 【参考方案1】:

NSError 在 Swift 3.0 中重命名为 Error 这可能会解决您的问题。 这段代码为我编译:

func calculateSegmentDirections(index: Int,
                            time: TimeInterval, routes: [MKRoute]) 

    let request: MKDirectionsRequest = MKDirectionsRequest()
    request.source = locationArray[index].mapItem
    request.destination = locationArray[index+1].mapItem
    request.requestsAlternateRoutes = true
    request.transportType = .automobile

    let directions = MKDirections(request: request)
    directions.calculate (completionHandler: 
        (response: MKDirectionsResponse?, error: Error?) in
        if let routeResponse = response?.routes 
            let quickestRouteForSegment: MKRoute =
                routeResponse.sorted(by: $0.expectedTravelTime <
                    $1.expectedTravelTime)[0]

            var timeVar = time
            var routesVar = routes

            routesVar.append(quickestRouteForSegment)
            timeVar += quickestRouteForSegment.expectedTravelTime

            if index+2 < self.locationArray.count 
                self.calculateSegmentDirections(index+1, time: timeVar, routes: routesVar)
             else 
                self.showRoute(routesVar, time: timeVar)
                self.hideActivityIndicator()
            
         else
            if let _ = error 
                let alert = UIAlertController(title: nil,
                                          message: "Directions not available.", preferredStyle: .alert)
                let okButton = UIAlertAction(title: "OK",
                                     style: .Cancel)  (alert) -> Void in
                                            self.navigationController?.popViewControllerAnimated(true)
            
            alert.addAction(okButton)
            self.presentViewController(alert, animated: true,
                                   completion: nil)
        
    )

【讨论】:

以上是关于从 Swift 2 转换为 Swift 3 时出错的主要内容,如果未能解决你的问题,请参考以下文章

使用 Alamofire 搜索从 swift 2 转换为 swift 3 时出错

将 json 和 HTTP“post”代码转换为 Swift 3 时出错

我必须将目标 c 代码转换为 swift 3.0,尝试在线转换时出错

将 JSON 转换为 Dictionary Swift 时出错

尝试从 Swift 2.0 中的 json 解析数据时出错?

无法转换任何类型?要键入 AnyObject?从 swift 3 转换为 swift 4 时