【发布时间】:2020-12-06 01:47:10
【问题描述】:
我不知道为什么会这样或如何解决它。错误:线程 1:EXC_BAD_ACCESS (code=2, address=0x7ffee84eaf60) 发生在我的行上:
let locationManager = CLLocationManager()
我在 viewDidLoad() 上面的类中声明了它。我已经运行这个模拟器很多次没有这个问题,现在它随机弹出。我检查以确保它不是我添加的最新代码,在没有所述代码的情况下再次运行它,并且错误仍然存在。以防万一您想到这一点,并不是我的默认位置设置为无,我之前遇到过该错误并已修复。以下是使用 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", style: .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, animated: true, 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
}
}
我不知道是什么原因造成的,因此感谢您的帮助:)
【问题讨论】:
-
你能显示崩溃的线程...以防它有任何提示吗? (调试窗口中的“bt”。)
-
@PhillipMills 不确定您所说的“bt”是什么意思,但我的调试窗口左侧有这个:self Jumper.ViewController 0x0000618000378480 UIKit.UIViewController UIViewController mapView MKMapView?无 tapToAddJumpSpotLabel UILabel? nil 无 locationManager CLLocationManager 0x0000000000000000 ObjectiveC.NSObject NSObject jumpSpotCreatorController Jumper.JumpSpotCreatorController 0x0000000000000000 userIsAllowedToAddAnnotation Bool false AND 在右边只是(11db)
-
我的意思是,当它崩溃时,在 (lldb) 提示符下键入“bt”。
-
@PhillipMills 好的,它给了我很多行,这是开始:线程 #1,队列 = 'com.apple.main-thread',停止原因 = EXC_BAD_ACCESS(代码 = 2,地址=0x7ffee244fb70) 帧#0: 0x000000010d11ab4a libclang_rt.asan_iossim_dynamic.dylib
__sanitizer_mz_malloc + 58 frame #1: 0x00007fff51b732a3 libsystem_malloc.dylibmalloc_zone_malloc + 104 帧#2: 0x00007fff23de0d50 CoreFoundation__CFStringChangeSizeMultiple + 848 frame #3: 0x00007fff23dda33e CoreFoundation+ 654 行不知道有多大帮助> -
代码中是否有提及某些内容的行?那些说在处理字符串时内存分配出了问题……这不是很具体。
标签: swift xcode core-location cllocationmanager exc-bad-access