Search code examples
iosswiftxcodemapkitallocation

Swift Maps: Unexpectedly found nil while implicitly unwrapping an Optional value


I'm using the location display feature

when run my application it show me this message->

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file

and here is my code

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
        }
    }
}

also I add ti info.plist -> info.plist and here is image of error that I get myBuild


Solution

  • here is miss to put that ->

    locationManager = CLLocationManager()

    because it just do it like -> var locationManager: CLLocationManager!

    so the value of locationManager will be will but when add the -> locationManager = CLLocationManager() -> you will get the value of 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
            }
        }
    }