如何解决线程1:我的CLLocationManager的EXC_BAD_ACCESS代码= 2,地址= 0x7ffee84eaf60
我不知道为什么会这样或如何解决。错误:线程1:EXC_BAD_ACCESS(代码= 2,地址= 0x7ffee84eaf60)出现在我的行上:
let locationManager = CLLocationManager()
我在viewDidLoad()以上的类中声明了它。我已经无数次运行此模拟器了很多次,现在它随机弹出了。我检查了一下是否是我添加的最新代码,方法是在没有上述代码的情况下再次运行它,并且错误仍然存在。以防万一,您并不是将我的默认位置设置为none,而是在之前遇到了该错误并进行了修复。这是使用locationManager的代码的所有部分:
let locationManager = CLLocationManager()
然后:
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
mapView.delegate = self
mapView.showsUserLocation = true
tapToAddJumpSpotLabel.isHidden = true
}
然后:
@IBAction func allowLocationButtonpressed(_ sender: UIBarButtonItem) {
switch CLLocationManager.authorizationStatus() {
// Case when authorization not determined.
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
// Case when authorization granted.
case .authorizedAlways,.authorizedWhenInUse:
let alertController = UIAlertController(title: "Location Access Granted",message: "We already have access to your location. If you want to change this,go to your settings app,and change our location access.",preferredStyle: .alert)
let okayAction = UIAlertAction(title: "Okay",style: .cancel,handler: nil)
alertController.addAction(okayAction)
self.present(alertController,animated: true,completion: nil)
// Case when authorization denied or restricted.
case .restricted,.denied:
let alertController = UIAlertController(title: "Location Access disabled",message: "We need access to your location in order to provide you with cliff jumping spots.",preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel",handler: nil)
let openAction = UIAlertAction(title: "Open Settings",style: .default) { (action) in
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url,options: [:],completionHandler: nil)
}
}
alertController.addAction(cancelAction)
alertController.addAction(openAction)
self.present(alertController,completion: nil)
// Default (Apple has possible future cases to be added to CLAuthorizationStatus).
@unkNown default:
return
}
}
最后:
//MARK: - CLLocationManagerDelegate Methods
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager,didFailWithError error: Error) {
if let error = error as? CLError,error.code == .denied {
locationManager.stopUpdatingLocation()
print(error)
return
}
}
func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
let lastLocation = locations.last!
mapView.centerCoordinate.latitude = lastLocation.coordinate.latitude
mapView.centerCoordinate.longitude = lastLocation.coordinate.longitude
}
}
我不知道是什么原因造成的,因此不胜感激:)
解决方法
嗯,经过进一步的挖掘,我感到很愚蠢。只是一个旧对象在另一个类中进行初始化,反之亦然,导致无限循环。好东西是其中之一很旧,我只是忘了删除它,所以去吧!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。