gpt4 book ai didi

swift - 为什么是locationManager(:didUpdateLocations) not called

转载 作者:行者123 更新时间:2023-11-30 10:52:58 27 4
gpt4 key购买 nike

我编写了以下测试代码

 @IBAction func btnLocation(_ sender: NSButton) {
let locManager = CLLocationManager()
if CLLocationManager.authorizationStatus() != .authorized {
print("not authorized")
}
locManager.delegate = self
locManager.desiredAccuracy = kCLLocationAccuracyBest
locManager.startUpdatingLocation()
locManager.requestLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
print(location.coordinate.latitude)
manager.stopUpdatingLocation()
let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(location, completionHandler: {(places, error) in
let place: CLPlacemark = places![0]
print(place.administrativeArea!)
})
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
print("authorization changed")
}

但是 locationManager(:didUpdateLocations) 没有被调用,也没有打印任何错误。如果我删除 locationManager(:didFailWithError) 方法,而不是开始工作,并且打印位置坐标,但 Xcode 还声称“委托(delegate)必须响应 locationManager:didFailWithError:我的代码有什么问题吗?

最佳答案

你需要做

let locManager = CLLocationManager()

一个实例变量来保存委托(delegate)的强引用,您也需要其中 1 个

locManager.startUpdatingLocation()
locManager.requestLocation()

关于swift - 为什么是locationManager(:didUpdateLocations) not called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54271155/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com