Search code examples
swiftswift2ios9xcode7directions

MKDirections / calculateDirectionsWithCompletionHandler not working anymore in Swift 2.0


I have another problem now with MapKit / MKDirections.

After I got everything working fine, Swift 2.0 came out and the 'calculateDirectionsWithCompletionHandler' method that looks like this:

directions.calculateDirectionsWithCompletionHandler({(response:
            MKDirectionsResponse!, error: NSError!) in

            if error != nil {
                println("Error getting directions")
            } else {
                self.showRoute(response)
            }

        })

does not work any more. It's giving me this error on line 1:

'(MKDirectionsResponse!, NSError!) -> Void' is not convertible to 'MKDirectionsHandler' (aka '(Optional<MKDirectionsResponse>, Optional<NSError>) -> ()')

Before the update this worked perfectly fine.... Thank you in advance for your help! :-)


Solution

  • For swift 2.0:

    let directions = MKDirections(request: request)
    directions.calculateDirectionsWithCompletionHandler{
        response, error in
    
        guard let response = response else {
            //handle the error here
            return
           }
           self.showRoute(response)
        }
    

    For more Info refer THIS.