Piepants Piepants - 28 days ago 16
Swift Question

Exception trying to geocode an address

I've got the following code which results in an exception and I don't know why. Once the exception is hit in XCode I can't continue to find out what the exception is - what I mean is normally you can continue execution in XCode and you can examine the call stack to see what the reason for the exception is, but in this case when I click the Continue Program Execution button it just continually sticks at the particular line in the screenshot.

let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString("Infinite Loop,Cupertino,California", completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in
print("WTF")
} as! CLGeocodeCompletionHandler)


enter image description here

The project is linked with the MapKit framework.

Answer

Your syntax is all wrong. You shouldn't be trying to force-cast anything. Assuming Swift 3 you need:

let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString("Infinite Loop,Cupertino,California", completionHandler: { (placemark: [CLPlacemark]?, error: Error?) -> Void in
    // do stuff
})

or even more simply:

let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString("Infinite Loop,Cupertino,California") { (placemark: [CLPlacemark]?, error: Error?) -> Void in
    // do stuff
}
Comments