我正在尝试等待一个异步执行的课程,然后继续,但没有运气。要点:
我看过:24725059,特别好25634068,但未能为我的情况制定答案。
大多数答案本质上是说要在完成处理程序中执行逻辑,但我想最终执行的逻辑是prepareForSegue。
我只是在学习,所以我很可能是因为我对其他概念中的完成处理程序缺乏了解。
var locationPlacemark: CLPlacemark!
@IBOutlet weak var myLocation: UITextField!
@IBAction func myButton(sender: AnyObject) {
print("start button IBAction")
processGeo({ (placemark, error) -> Void in
self.locationPlacemark = placemark
print("setting placemark")
})
print("end button IBAction")
}
func processGeo(getLocCompletionHandler: ((placemark : CLPlacemark?, error : NSError?) -> Void)!) {
print("before CLGeocoder")
CLGeocoder().geocodeAddressString(self.myLocation.text!, completionHandler: {(placemarks, error) -> Void in // called after completes
if((error) != nil){
print("Error", error)
}
print("after CLGeocoder")
if let placemark = placemarks?[0] {
getLocCompletionHandler(placemark: placemark, error: error)
print("after calling completion handler")
}
})
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "InfoPostingMapSegue" {
let vc = segue.destinationViewController as! InfoPostingMapViewController
print("in prepareForSegue")
vc.locationPlacemark = locationPlacemark
}
}
控制台打印是:
start button IBAction
before CLGeocoder
end button IBAction
in prepareForSegue
after CLGeocoder
setting placemark
after calling completion handler