objective-c - 没有互联网时位置管理器崩溃

标签 objective-c swift

当用户连接到互联网时,程序运行正常。但是,当用户未连接时,应用程序崩溃并显示:

Error:The operation couldn’t be completed. (kCLErrorDomain error 8.)
fatal error: unexpectedly found nil while unwrapping an Optional value

我想在它崩溃之前捕获错误并显示一个警报 Controller

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in
        if (error != nil) {
            println("Error:" + error.localizedDescription)
        }
        if placemarks.count > 0 {
            let pm = placemarks[0] as CLPlacemark
            self.displayLocationInfo(pm)
            currentLoc = manager.location
            currentLocGeoPoint = PFGeoPoint(location:currentLoc)
            var query = PFQuery(className:"Restaurant") //default: Restaurant
            query.whereKey("RestaurantLoc", nearGeoPoint:currentLocGeoPoint, withinMiles:setDistanceMiles) //filter by miles
            query.limit = 1000 //limit number of results
            query.findObjectsInBackgroundWithBlock {
                (objects: [AnyObject]!, error: NSError!) -> Void in
                if objects.isEmpty {
                    //NO RESULTS
                    let alertController = UIAlertController(title: "Unavailable Area", message:
                        "This app is not available in your area yet. We'll try our best to have it up and running as soon as possible.", preferredStyle: UIAlertControllerStyle.Alert)
                    alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler: nil))
                    self.presentViewController(alertController, animated: true, completion: nil)
                    self.navigationController!.popViewControllerAnimated(true)
                } else {
                    newArray = objects
                }
                self.hideActivityIndicator(self.view)
            }
        } else {
            println("error: \(error)")
        }
    })
}

enter image description here

最佳答案

问题是您的if 语句。你是这样说的:

if (error != nil) {
    println("Error:" + error.localizedDescription)
}
if placemarks.count > 0 { // ...

嗯, 有一个错误。你被告诉有一个错误。但是你不听!你没有停下来!您就好像什么都没发生一样继续,并尝试访问 placemarks 变量——它没有被返回。您需要在此处使用逻辑,以便在出现错误时退出:

if (error != nil) {
    println("Error:" + error.localizedDescription)
    return // go no further!
}
if placemarks.count > 0 { // ...

但是请注意,最好先检查 placemarks 是否为 nil :

if placemarks == nil {
    // check error
    // return
}
if placemarks.count > 0 { // ...

关于objective-c - 没有互联网时位置管理器崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28182247/

相关文章:

ios - 如何在 SwiftUI 中保留 View 结构列表?

ios - 如何在不使用 segue 的情况下快速导航并将数据传递到另一个 View Controller

ios - ScrollView 不适用于 iPad 但适用于 iPhone

ios - Cocoa Touch Objective-C Framework 中的 Swift 类

objective-c - Xcode 4.2 codesense - 你如何缩小建议的完成列表?

objective-c - iOS 上的自定义键盘 : How do I access the UITextField?

json - 读/写本地 json 文件 swift 4

ios - 如何快速打开 View Controller 而不执行其进程?

objective-c - For循环,等待外部方法完成

ios - 扩展对 swift 的性能有害吗?