因此,我试图让它在完成处理程序之后执行一些操作,但我一直收到一个错误:
预期CLGeocodeCompletionHandler
class func didCompleteGeocoding(placemarks: [CLPlacemark]?, error: NSError?, completion: (results: Bool) -> Void){
if error == nil && placemarks!.count > 0 {
let placemark = placemarks![0] as! CLPlacemark
let geocodedLocation = placemark.location!
//some more code here
}
var geoCoder = CLGeocoder()
var placemarks: [CLPlacemark]?
func getlocation(){
let cordinates = geoCoder.geocodeAddressString(locationtext, completionHandler: toSearchandPost2.didCompleteGeocoding(placemarks, error: nil, completion: { (results) -> Void in
if results == true{
// do something here
}
}))
}
最佳答案
不能直接使用该函数。请查看以下代码:
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)"
}
})
}
你需要这样称呼它:
yourModel.getAndDisplayLocationStringForLocation(someLocation) { (string: String?, error: NSError?) -> () in
if (error == nil) {
self.textField.text = string
}
}
请在下面的链接完整回答。不过,上面的代码就足够了。
参考:iOS Swift - CLGeocoder completionHandler block
关于ios - CLGeocodeCompletionHandler,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34489111/