1

This is the second and last section I am battling with converting Swift 2 to Swift 3

The old working code was

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) } }) } 

The converted code is

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) } }) } 

It throws an error on the line

directions.calculateDirectionsWithCompletionHandler ({ 

The error is

Cannot convert value of type '(MKDirectionsResponse?, NSError?) -> ()' to expected argument type 'MKDirectionsHandler' (aka '(Optional, Optional) -> ()')

If anyone can help me I would be very thankful!!

1
  • Those functions are far too long. You should refactor them first before converting. Then it will make converting them much easier. Commented Sep 21, 2016 at 9:55

1 Answer 1

4

NSError was renamed to Error in Swift 3.0 This may fix your issue. This code compiles for me:

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) } }) } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.