iOS Swift - CLGeocoder completionHandler block

你离开我真会死。 提交于 2019-12-19 04:57:07

问题


I'm trying to parse a location (CLLocation) into a String.

    func locationToString (currentLocation: CLLocation) -> String? {
    var whatToReturn: String?
    CLGeocoder().reverseGeocodeLocation(currentLocation, completionHandler: { (placemarks: [AnyObject]!, error: NSError!) in
        if error == nil && placemarks.count > 0 {
            let location = placemarks[0] as CLPlacemark
            whatToReturn = "\(location.locality) \(location.thoroughfare) \(location.subThoroughfare)"

        }
    })
    return whatToReturn
}

Obviously, whatToReturn always returns null, because completionHandler runs in the background. I'm having a hard time understanding how do I update my String when completionHandler finishes?

Thanks.


回答1:


If you want to use your string in a textField, like indicated in your comments, do this:

func getAndDisplayLocationStringForLocation(currentLocation: CLLocation) {
    CLGeocoder().reverseGeocodeLocation(currentLocation, completionHandler: { (placemarks: [AnyObject]!, error: NSError!) in
        if error == nil && placemarks.count > 0 {
            let location = placemarks[0] as CLPlacemark
            self.textField.text = "\(location.locality) \(location.thoroughfare) \(location.subThoroughfare)"

        }
    })
}

However, if you need access elsewhere, perhaps pass a closure as an arg:

func getAndDisplayLocationStringForLocation(currentLocation: CLLocation, withCompletion completion: (string: String?, error?, error: NSError?) -> ()) {
    CLGeocoder().reverseGeocodeLocation(currentLocation, completionHandler: { (placemarks: [AnyObject]!, error: NSError!) in
        if error == nil && placemarks.count > 0 {
            let location = placemarks[0] as CLPlacemark
            completion(string: "\(location.locality) \(location.thoroughfare) \(location.subThoroughfare)", error: nil)

        } else {
            completion(nil, error)
        }
    })
}

Then call like this:

yourModel.getAndDisplayLocationStringForLocation(someLocation) { (string: String?, error: NSError?) -> () in
    if (error == nil) {
        self.textField.text = string
    }
}

You may want to handle error etc. differently. This should be enough to get you started.



来源:https://stackoverflow.com/questions/27749854/ios-swift-clgeocoder-completionhandler-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!