What I'm trying to do is pass a CLLocation
to the function getPlacemarkFromLocation
which then uses the passed CLLocation
through reverseGeocodeLocation
to set the CLPlacemark?
that will be returned.
I'm having issues creating the completionHandler
closure in reverseGeocodeLocation
, it's throwing a compiler error/crash:
In Swift, CLGeocodeCompletionHandler
is CLGeocodeCompletionHandler = (AnyObject[]!, NSError!) -> Void
according to the documentation AnyObject[]!
is supposed to contain CLPlacemark
objects just like the Objective-C version.
Here's my current code:
class func getPlacemarkFromLocation(location:CLLocation)->CLPlacemark?{
var g = CLGeocoder()
var p:CLPlacemark?
g.reverseGeocodeLocation(location, completionHandler: {
(placemarks, error) in
let pm = placemarks as? CLPlacemark[]
if (pm && pm?.count > 0){
p = placemarks[0] as? CLPlacemark
}
})
return p?
}
EDIT: It seems like the error had to do with placemarks.count
with placemarks
not being treated like an array. It compiles now, however I'm getting nothing but nil when trying to set p
inside the completionHandler
. I've checked the CLLocation
s being passed and they are valid.
EDIT 2: After printing placemarks
, I can confirm that it returns data. However p
is still returning nil.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…