gpt4 book ai didi

当尝试查找用户位置 "Terminating app due to uncaught exception ' NSRangeException' 时,Swift 有时会给出此 AppDelegate 错误..”

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

当尝试查找用户位置时,Swift 有时会出现此 AppDelegate 错误

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray'
*** First throw call stack:
(0x18b3fd1b8 0x189e3455c 0x18b367bac 0x1000ffd20 0x1000fe7f4 0x1000fea30 0x193400a38 0x1934002b4 0x1933f34b8 0x18b3aaa44 0x18b3aa240 0x18b3a8538 0x18b2d62b8 0x18cd8a198 0x19131d7fc 0x191318534 0x100108078 0x18a2b95b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

这是我的 locationManager 函数:

 override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
myLat = (location?.coordinate.latitude)!
myLng = (location?.coordinate.longitude)!
self.locationManager.stopUpdatingLocation()
createMarker(lat: myLat,lng: myLng,zoom: 16)
}

有时它运行良好,但有时应用程序崩溃并给出此 AppDelegate 错误。 那么为什么我会收到此错误以及如何修复它?

最佳答案

您假设 [CLLocation] 数组实际上具有元素,但这并不总是正确的。添加 if let 以根据是否提供位置来有条件地处理该位置。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
myLat = location.coordinate.latitude
myLng = location.coordinate.longitude
self.locationManager.stopUpdatingLocation()
createMarker(lat: myLat, lng: myLng, zoom: 16)
}
}

关于当尝试查找用户位置 "Terminating app due to uncaught exception ' NSRangeException' 时,Swift 有时会给出此 AppDelegate 错误..”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44003190/

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