【发布时间】:2016-03-06 03:34:48
【问题描述】:
我有一个 locationManager 函数来获取用户的当前位置并发布城市和州的名称。我有一个打印语句,因此我可以在控制台中检查一切是否正常……确实如此。但是,它会打印 3 次城市位置。这实际上会导致我的实际应用出现问题,但这超出了这个问题的重点。
我的功能如下:
var usersLocation: String!
var locationManager: CLLocationManager!
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0]
CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) -> Void in
if error != nil {
print(error)
} else {
let p = placemarks?.first // ".first" returns the first element in the collection, or nil if its empty
// this code above will equal the first element in the placemarks array
let city = p?.locality != nil ? p?.locality : ""
let state = p?.administrativeArea != nil ? p?.administrativeArea : ""
self.navigationBar.title = ("\(city!), \(state!)")
self.usersLocation = ("\(city!), \(state!)")
self.locationManager.stopUpdatingLocation()
print(self.usersLocation)
self.refreshPosts()
}
}
}
所以在我的 print(self.usersLocation) 中,它将在我的控制台中打印三遍。这正常吗?
更新以显示 VIEWDIDLOAD
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 250.0
}
【问题讨论】:
-
你有一个调试器。调试!在您的
print语句中放置一个断点,以便您每次都停在那里。现在您可以查看 调用堆栈 并了解为什么要调用您的didUpdateLocations。 -
@matt 我应该提到我在编码方面是一个完全的菜鸟。我了解如何设置断点以使其停止,但是在哪里可以找到此调用堆栈并开始调试此问题?谢谢。
-
当你命中断点时,你会在调试导航器中看到调用堆栈。
标签: ios swift locationmanager reversegeocodelocation