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!!