【问题标题】:Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value using CoreLocation线程 1:致命错误:在使用 CoreLocation 隐式展开可选值时意外发现 nil
【发布时间】:2020-11-01 23:55:18
【问题描述】:

我真的是 swift 新手,我想弄清楚为什么我在运行这个程序时得到一个 nil 值。我尝试添加func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [CLLocation]!),但这似乎不起作用。任何帮助将不胜感激。谢谢

这里是代码(PS:这是变量locManager的值:var locManager = CLLocationManager()

override func viewDidLoad() {
        super.viewDidLoad()
        tableView.backgroundColor = UIColor.systemGreen
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableView.automaticDimension
        
        locManager.requestWhenInUseAuthorization()
        func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [CLLocation]!) {
            let locationZoom = locations.last as! CLLocation
            
        }
        if CLLocationManager.authorizationStatus() == .authorizedWhenInUse || CLLocationManager.authorizationStatus() ==  .authorizedAlways
        {
            currentLocation = locManager.location
            //getting locations
            let longa = currentLocation.coordinate.longitude ***<- (Error occurring at this line)*** 
            let latta = currentLocation.coordinate.latitude
            nearbyLocations1(latitude: latta, longitude: longa) { (longitude, latitude, name, vicinity) in
                       
                    print("name 1 is ", name)
                   }
            nearbyLocations2(latitude: latta, longitude: longa) { (longitude, latitude, name, vicinity) in
                print("name 2 is ", name)
            }
            
        } 
        
    
    }

【问题讨论】:

    标签: ios swift xcode core-location cllocationmanager


    【解决方案1】:

    可能 currentLocation = nil。使用

    if let currentLocation = locManager.location {
        // Code for currentLocation != nil
    } else {
        // Code for currentLocation == nil
    }
    

    或类似的东西。

    【讨论】:

    • 这似乎不起作用。程序没有显示错误,但是代码没有被执行
    【解决方案2】:

    主要有两个问题:

    1. 方法didUpdateLocations必须声明在类的顶层(与viewDidLoad相同的级别)并且所有处理位置的代码都必须在这个方法内

      func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [CLLocation]!) {
           guard let currentLocation = locations.last else { return }
           //getting locations
           let longa = currentLocation.coordinate.longitude 
           let latta = currentLocation.coordinate.latitude
           nearbyLocations1(latitude: latta, longitude: longa) { (longitude, latitude, name, vicinity) in                       
               print("name 1 is ", name)
           }
           nearbyLocations2(latitude: latta, longitude: longa) { (longitude, latitude, name, vicinity) in
               print("name 2 is ", name)
           }
      }
      
    1. 要获取位置,您必须将位置经理的代表设置为self 并调用startUpdatingLocation()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 2020-09-03
      • 2020-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多