Search code examples
iosmkmapviewswiftmapkitroutes

Completion Handler in Swift


I have been searching for many hours trying to find the solution to this closure problem in swift. I have found many resources for explaining the closures but for some reason I can't seem to get this working.

This is the Objective-C code I am trying to convert into swift:

[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
            NSLog(@"%@",[response description]);
            NSLog(@"%@",[error description]);

            }];

and the swift I am trying but is not working:

directions.calculateDirectionsWithCompletionHandler(response: MKDirectionsResponse?, error: NSError?) {
    println(response.description)
    println(error.description)
}

directions is an MKDirections object.

Thanks!


Solution

  • Try

    directions.calculateDirectionsWithCompletionHandler ({
    (response: MKDirectionsResponse?, error: NSError?) in 
            println(response?.description)
            println(error?.description)
        })