0

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

pkamb
  • 33,281
  • 23
  • 160
  • 191
jeena azeez
  • 388
  • 1
  • 3
  • 12
  • Where are you setting `locationManager`? The message is telling you that it hasn't been set at the time that `setupLocationManger` is called. – bg2b Apr 02 '20 at 21:34
  • I got it,Thank you – jeena azeez Apr 02 '20 at 23:33
  • 1
    Does this answer your question? [What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – pkamb Jun 07 '20 at 21:07

1 Answers1

1

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
        }
    }
}
jeena azeez
  • 388
  • 1
  • 3
  • 12