Swift地图。在隐式解包一个可选值时意外发现nil。

问题描述 投票:0回答:1

我正在使用位置显示功能

当运行我的应用程序时,它显示了这个消息->

Fatal error: 当隐式解包一个可选值时,意外地发现了nil:文件。

这是我的代码

import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
    @IBOutlet weak var mymap: MKMapView!
    var locationManager: CLLocationManager!

    override func viewDidLoad() {
          checkLocationSevices()
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    func setupLocationManger()
    {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
    }

    func checkLocationSevices()
    {
        if CLLocationManager.locationServicesEnabled()
        {
            setupLocationManger()
            checkLocationAuthorizatiob()
        }
        else
        {

        }
    }
    func checkLocationAuthorizatiob()
    {
        switch CLLocationManager.authorizationStatus()
        {
        case .authorizedWhenInUse:
            mymap.showsUserLocation = true
            break
        case .denied:
            break
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
            break
        case .restricted:
            break
        case .authorizedAlways:
            break

         default:
            break
        }
    }
}

我还添加了 ti info.plist ->info.plist这里是我得到的错误图像myBuild

ios swift xcode mapkit allocation
1个回答
0
投票

这里是我的......这里错过了放-&gt。

locationManager = CLLocationManager()

因为它只是像-&gt.这样做。 var locationManager: CLLocationManager!

所以locationManager的值将是will,但当添加-> locationManager = CLLocationManager() 但当添加->时,你将会得到locationManager的值

import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
    @IBOutlet weak var mymap: MKMapView!
    var locationManager: CLLocationManager!

    override func viewDidLoad() {
      locationManager = CLLocationManager()
          checkLocationSevices()
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    func setupLocationManger()
    {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
    }

    func checkLocationSevices()
    {
        if CLLocationManager.locationServicesEnabled()
        {
            setupLocationManger()
            checkLocationAuthorizatiob()
        }
        else
        {

        }
    }
    func checkLocationAuthorizatiob()
    {
        switch CLLocationManager.authorizationStatus()
        {
        case .authorizedWhenInUse:
            mymap.showsUserLocation = true
            break
        case .denied:
            break
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
            break
        case .restricted:
            break
        case .authorizedAlways:
            break

         default:
            break
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.