Swift - CLGeocoder reverseGeocodeLocation completionHandler闭包 [英] Swift - CLGeocoder reverseGeocodeLocation completionHandler closure

查看:102
本文介绍了Swift - CLGeocoder reverseGeocodeLocation completionHandler闭包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的是将 CLLocation 传递给函数 getPlacemarkFromLocation 然后使用传递的 CLLocation 通过 reverseGeocodeLocation 设置将返回的 CLPlacemark?

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.

我在 reverseGeocodeLocation completionHandler 闭包时遇到问题c>,它抛出编译器错误/崩溃:

I'm having issues creating the completionHandler closure in reverseGeocodeLocation, it's throwing a compiler error/crash:


在Swift中, CLGeocodeCompletionHandler CLGeocodeCompletionHandler =(AnyObject [] !, NSError!) - > Void 根据文档 AnyObject []!应该包含 CLPlacemark 对象就像Objective-C版本。

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.

这是我目前的代码:

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?
}

编辑:似乎错误与 placemarks.count ,地标未被视为数组。它现在编译,但是当我尝试在 completionHandler 中设置 p 时,我只得到nil。我检查了传递的 CLLocation ,它们是有效的。

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 CLLocations being passed and they are valid.

编辑2:打印后地标,我可以确认它返回数据。但是 p 仍然返回nil。

EDIT 2: After printing placemarks, I can confirm that it returns data. However p is still returning nil.

推荐答案

我找到了答案我此主题中需要:使用reverseGeocodeLocation设置地址字符串:并从方法返回

I found the answer I needed in this thread: Set address string with reverseGeocodeLocation: and return from method

问题在于 reverseGeocodeLocation 是异步的,该方法在completionBlock设置<$之前返回一个值c $ c> p 在我的例子中。

The issue lies with the fact that reverseGeocodeLocation is asynchronous, the method is returning a value before the completionBlock sets p in my example.



根据要求,这是我当前的代码。


As requested, here's my current code.

func showAddViewController(placemark:CLPlacemark){
    self.performSegueWithIdentifier("add", sender: placemark) 
}

func getPlacemarkFromLocation(location: CLLocation){
    CLGeocoder().reverseGeocodeLocation(location, completionHandler:
        {(placemarks, error) in
            if error {println("reverse geodcode fail: \(error.localizedDescription)")}
            let pm = placemarks as [CLPlacemark]
            if pm.count > 0 { self.showAddPinViewController(placemarks[0] as CLPlacemark) }
    })
}

我不想采用NSNotificationCenter路由,因为这会增加不必要的开销,而是在 completionHandler 闭包内调用另一个函数并传递 CLPlacemark getPlacemarkFromLocation 生成,作为保持异步的参数,因为该函数将在 placemarks之后调用设置函数(应该)接收所需的地标并执行您想要的代码。希望我所说的是有道理的。

I didn't want to take the NSNotificationCenter route because that would add unnecessary overhead, rather inside the completionHandler closure I call upon another function and pass the CLPlacemark generated by getPlacemarkFromLocation as a parameter to keep things asynchronous since the function will be called after placemarks is set the function (should) receive the placemark needed and execute the code you want. Hope what I said makes sense.

这篇关于Swift - CLGeocoder reverseGeocodeLocation completionHandler闭包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆