我不會打印在控制檯currentCity,對於使用城市名稱的另一個功能!請幫幫我。我如何利用變量從功能其他功能SWIFT 3
var locationManager = CLLocationManager()
var currentCity: String?
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
print(currentCity) //----error not printing CITY NAME
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0]
CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) in
if error != nil {
print(error?.localizedDescription as Any)
} else {
if let placemark = placemarks?[0] {
if let city = placemark.addressDictionary?["City"] as? NSString {
self.cityLbl.text = city as String
self.currentCity = city as String
}
}
}
}
}
將打印線在'didUpdateLocations'委託方法在'else'範圍的端部。 – vadian
我需要在另一個功能中使用城市名稱!你的變體已經工作,但我不需要在didUpdateLocations中打印。我不會把這個城市名稱,並在json –
中使用這個名字認爲異步並從委託方法調用*其他函數*。 – vadian