swift 闭包中的变量范围 wrt CLGeocoder()
swift variable scope in closure wrt CLGeocoder()
谁能帮我理解为什么下面的代码会有这样的输出:
[<+37.49638550,-122.31160280> +/- 5.00m (speed 32.65 mps / course
294.26) @ 7/27/16, 4:34:19 AM Eastern Daylight Time]
n/a Belmont Belmont
Belmont 1
也就是说,为什么变量 locality
在特定点打印时的值为 "n/a" 而 localityVC
& localityGlobal
从不做?这纯粹是范围问题还是与 CLGeocoder()
有关?谢谢
import UIKit
import MapKit
import CoreLocation
var localityGlobal: String = "n/a"
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var localityVC: String = "n/a"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
print("hello")
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
let userLocation: CLLocation = locations[0]
var locality: String = "n/a"
CLGeocoder().reverseGeocodeLocation(userLocation, completionHandler: {(placemarks, error) -> Void in
if error != nil {
print("CLGeocoder.reverseGeocodeLocation() failed")
return
}
if placemarks!.count > 0 {
locality = placemarks![0].locality!
self.localityVC = placemarks![0].locality!
localityGlobal = placemarks![0].locality!
print(locality, placemarks!.count)
} else {
print("CLGeocoder.reverseGeocodeLocation() error in geocoder data")
}
})
print(locality, localityVC, localityGlobal)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
为了使上述 运行 您还需要:
Build Phases -> Link Binary with Libraries
包括
CoreLocation.framework
,
Info.plist
必须有变量
NSLocationWhenInUseUsageDescription
和
NSLocationAlwaysUsageDescription
集,
Simulator -> Debug -> Location
设置为 Apple
、City Bicycle Ride
、City Run
或 Freeway Drive
我 运行宁 xcode 7.3.1 (7D1014)
这不是范围问题,而是时间问题。您得到 n/a 是因为在执行该打印语句时闭包尚未 运行 。
其他变量保留了先前调用的值。
谁能帮我理解为什么下面的代码会有这样的输出:
[<+37.49638550,-122.31160280> +/- 5.00m (speed 32.65 mps / course 294.26) @ 7/27/16, 4:34:19 AM Eastern Daylight Time]
n/a Belmont Belmont
Belmont 1
也就是说,为什么变量 locality
在特定点打印时的值为 "n/a" 而 localityVC
& localityGlobal
从不做?这纯粹是范围问题还是与 CLGeocoder()
有关?谢谢
import UIKit
import MapKit
import CoreLocation
var localityGlobal: String = "n/a"
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var localityVC: String = "n/a"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
print("hello")
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
let userLocation: CLLocation = locations[0]
var locality: String = "n/a"
CLGeocoder().reverseGeocodeLocation(userLocation, completionHandler: {(placemarks, error) -> Void in
if error != nil {
print("CLGeocoder.reverseGeocodeLocation() failed")
return
}
if placemarks!.count > 0 {
locality = placemarks![0].locality!
self.localityVC = placemarks![0].locality!
localityGlobal = placemarks![0].locality!
print(locality, placemarks!.count)
} else {
print("CLGeocoder.reverseGeocodeLocation() error in geocoder data")
}
})
print(locality, localityVC, localityGlobal)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
为了使上述 运行 您还需要:
Build Phases -> Link Binary with Libraries
包括CoreLocation.framework
,Info.plist
必须有变量NSLocationWhenInUseUsageDescription
和NSLocationAlwaysUsageDescription
集,Simulator -> Debug -> Location
设置为Apple
、City Bicycle Ride
、City Run
或Freeway Drive
我 运行宁 xcode 7.3.1 (7D1014)
这不是范围问题,而是时间问题。您得到 n/a 是因为在执行该打印语句时闭包尚未 运行 。
其他变量保留了先前调用的值。