gpt4 book ai didi

ios - CLLocationManager.authorizationStatus 导致 EXC_BAD_ACCESS code=2

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:58:25 25 4
gpt4 key购买 nike

如果允许定位服务,我的第一个应用到目前为止运行良好。

只要我特别禁用此应用的定位服务(飞行模式,以及通常禁用的定位服务都按预期工作)。

The Error at runtime

代码如下:

    func locationServices()->Bool{
if CLLocationManager.locationServicesEnabled() {
switch(CLLocationManager.authorizationStatus()) {
case .NotDetermined, .Restricted, .Denied:
return false
case .AuthorizedAlways, .AuthorizedWhenInUse:
return true
}
} else {
return false
}

}

从 viewDidLoad 调用:

    override func viewDidLoad() {
super.viewDidLoad()

txtNotes.delegate = self
datePicker.maximumDate = NSDate()
if (inc != nil) {
let Dateformatter = NSDateFormatter()
Dateformatter.dateStyle = NSDateFormatterStyle.MediumStyle
navigationItem.title = Dateformatter.stringFromDate((inc?.date)!)
datePicker.date = (inc?.date)!
txtNotes.text = inc?.notes
ratingControl.rating = (inc?.rating)!
lblAccuracy.text = inc?.geoloc
self.location = inc?.geocor
}
else {
if(locationServices()){
self.locationManager = CLLocationManager()
locationManager!.delegate = self
locationManager!.requestWhenInUseAuthorization()
locationManager!.desiredAccuracy = kCLLocationAccuracyBest
locationManager!.startUpdatingLocation()

}
else{
lblAccuracy.text = "Location Services Disabled"
let alertController = UIAlertController(
title: "Location Services Disabled",
message: "Please allow this application to use location services",
preferredStyle: .Alert)

let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
alertController.addAction(cancelAction)

let openAction = UIAlertAction(title: "Open Settings", style: .Default) { (action) in
if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
UIApplication.sharedApplication().openURL(url)
}
}
alertController.addAction(openAction)
self.presentViewController(alertController, animated: true, completion: nil)
viewDidLoad()
}
}
}

最佳答案

访问一个尚未初始化的委托(delegate)不是一个聪明的主意...

在访问它之前进行初始化解决了我的问题。

关于ios - CLLocationManager.authorizationStatus 导致 EXC_BAD_ACCESS code=2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38912678/

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