gpt4 book ai didi

swift - DidUpdateLocations 不存储位置,userLock 始终为 nil

转载 作者:行者123 更新时间:2023-11-30 11:23:41 28 4
gpt4 key购买 nike

我试图简单地获取用户当前的设备位置并将其存储在名为 userLoc 的全局变量中。我有这段代码来调用 didUpdateLocation:

self.locationManager.requestLocation()
print("aaa ", self.userLoc)

并将 userLoc 设置为当前位置:

func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
self.userLoc = location
}
}

userLoc 定义如下:

var userLoc: CLLocation?

但是当我运行应用程序并尝试打印 userLoc 时,它总是打印:

aaa nil

隐私 - 使用时的位置使用说明在 Info.plist 中设置。老实说,我不知道还能尝试什么。一段时间以来,我一直在用头撞墙,试图解决这个问题。如果您能提供任何帮助,我们将非常感激。谢谢。

最佳答案

这就是我在 View Controller 类中获取用户位置的方法。我认为您缺少 locationManager.startUpdatingLocation():

var currentLocation: CLLocationCoordinate2D!
var locationManager: CLLocationManager = CLLocationManager()

locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
currentLocation = locationManager.location!.coordinate

关于swift - DidUpdateLocations 不存储位置,userLock 始终为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50996409/

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