iOS Swift - CLGeocoder completionHandler 块
iOS Swift - CLGeocoder completionHandler block
我正在尝试将位置 (CLLocation) 解析为字符串。
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
}
显然,whatToReturn 总是 returns null,因为 completionHandler 在后台运行。
我很难理解如何在 completionHandler 完成时更新我的字符串?
谢谢。
如果您想在文本字段中使用您的字符串,如评论中所示,请执行以下操作:
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)"
}
})
}
但是,如果您需要在其他地方访问,也许可以将闭包作为 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)
}
})
}
然后这样调用:
yourModel.getAndDisplayLocationStringForLocation(someLocation) { (string: String?, error: NSError?) -> () in
if (error == nil) {
self.textField.text = string
}
}
您可能希望以不同方式处理错误等。这应该足以让您入门。
我正在尝试将位置 (CLLocation) 解析为字符串。
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
}
显然,whatToReturn 总是 returns null,因为 completionHandler 在后台运行。 我很难理解如何在 completionHandler 完成时更新我的字符串?
谢谢。
如果您想在文本字段中使用您的字符串,如评论中所示,请执行以下操作:
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)"
}
})
}
但是,如果您需要在其他地方访问,也许可以将闭包作为 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)
}
})
}
然后这样调用:
yourModel.getAndDisplayLocationStringForLocation(someLocation) { (string: String?, error: NSError?) -> () in
if (error == nil) {
self.textField.text = string
}
}
您可能希望以不同方式处理错误等。这应该足以让您入门。