fatal error: unexpectedly found nil while unwrapping an Optional value

Hi! I'm new in swift and xcode and I don'y know why there is an error in my code. Can someone help me please?


This is my code (I am trying to put a map and show the user's location):


It shows an error at the line 23:

fatal error: unexpectedly found nil while unwrapping an Optional value

(lldb)



import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {  
/
  
    @IBOutlet weak var map: MKMapView!
  
    let manager = CLLocationManager()
  
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
  
    {
      
        let location = locations[0]
      
        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
      
        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.latitude)
      
        let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
      
        map.setRegion(region, animated: true)
      
        self.map.showsUserLocation = true
  
    }


Thanks!

Replies

My guess is you've neglected to connect the map view in Interface Builder to the IBOutlet named 'map' in the class 'ViewController', and therefore 'map' contains nil when it is accessed.