我是 Swift 的新手,我需要在 viewDidLoad()
中设置 var postalCode
。正如您在下面的代码中看到的,我在 didUpdateLocations
中使用了反向地理编码位置,但由于它是异步的,因此 viewDidLoad()< 中未设置
。我该怎么做?postalCode
变量
ViewController.swift
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var postalCode = ""
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
println("Postal code is: \(self.postalCode)")
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)-> Void in
if error != nil {
println("Reverse geocoder failed with error: \(error.localizedDescription)")
return
}
if placemarks.count > 0 {
let placemark = placemarks[0] as! CLPlacemark
self.locationManager.stopUpdatingLocation()
self.postalCode = (placemark.postalCode != nil) ? placemark.postalCode : ""
println("Postal code updated to: \(self.postalCode)")
}else{
println("No placemarks found.")
}
})
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("Location manager error: \(error.localizedDescription)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
最佳答案
@MirekE 是正确的。您的 -viewDidLoad
方法不会等待位置管理器完成更新,然后等待地理编码器完成地理编码。它在设置 postalCode
值之前记录消息 Postal code is:
。
解决方案:
将您需要的任何代码移动到 postalCode
上的自定义 didSet
触发器中:
var postalCode:String! = "" {
didSet {
println("Postal code is: \(self.postalCode)")
// and whatever other code you need here.
}
};
关于swift - 如何在 Swift viewDidLoad 中获取邮政编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31095881/