我使用中间处理程序等待reverseGeocodeLocation
获取响应,以便我可以更新全局变量以供将来的视图控制器使用。即使在包含reverseGeocodeLocation
的函数中使用中间完成处理程序之后,主队列仍然在方法调用之前。
func getReversedGeocodeLocation(completionHandler: (String, NSError?) ->()){
CLGeocoder().reverseGeocodeLocation(newAddressLocation, completionHandler: {(placemarks, error) -> Void in
if error != nil {
print("Reverse geocoder failed with error" + error!.localizedDescription)
return
}
else if placemarks?.count > 0 {
print("reverseGeocode success")
}
else {
print("Problem with the data received from geocoder")
}
completionHandler(placemarks!.first!.name! ?? "", error)
})
}
呼叫getReversedGeocodeLocation
:
getReversedGeocodeLocation() { address, error in
if address != "" {
self.newAddress = address //newAddress is the variable
//I need to update
}
}