【发布时间】:2015-02-18 13:45:57
【问题描述】:
以下代码无法正常工作。
func convertToStreet(location:CLLocationCoordinate2D) -> CLPlacemark {
var tempLocation = CLLocation(latitude: location.latitude, longitude: location.longitude)
var temPlacemark:CLPlacemark?
CLGeocoder().reverseGeocodeLocation(tempLocation, completionHandler: {(placemarks, error) in
temPlacemark = (placemarks[0] as CLPlacemark)
println(temPlacemark!.thoroughfare)
})
return temPlacemark!
}
完成处理程序中的 Println 可以正常工作,但 temPlacemark 的值在代码末尾为 nil。为什么会这样?非常感谢您。
【问题讨论】:
-
发生这种情况是因为在返回时,它仍在处理它。如果您让线程等待一定的时间,您将获得正确的值。此外,如果您将断点放在完成处理程序内部和外部,您将看到外部部分首先执行。