I am playing around with some APIs in swift. I came across the Forecast.io API for weather, downloaded an objective-c wrapper, and created a bridging header in my xcode project. The only issue I am having is that I have a closure (objective-c block) that will not execute. Here is the code:
var geocoder:CLGeocoder = CLGeocoder()
geocoder.geocodeAddressString("1 Infinite Loop, Cupertino, CA", completionHandler: {objects, error in
if (objects.count >= 0) {
println("yes")
curLocPlacemark = objects[0] as? CLPlacemark
if(curLocPlacemark != nil) {
curLocation = curLocPlacemark!.location
}
} else {
println("no")
}
})
When debugging, the debugger gets to the line geocoder.geocodeAddressString("1 Infinite Loop, Cupertino, CA", completionHandler: {objects, error in
, and then skips over the rest of the lines showed. Is this just a dumb syntax error I can't find? Thanks!
The code inside the closure is executed asynchronously -- it's wrapped up and executed by the geocoder upon completion of the address string geocoding. (That's what a closure is -- wrapped up code and context for later execution.) Are you seeing the correct output in your log?