James Fu
James Fu

Reputation: 452

mapView.addAnnotation() says "unexpectedly found nil while unwrapping an Optional value"

I try to segue from the address of tableView cell to a mapView and show the pin on the map.

I'm sure everything is not nil in my code.

But Xcode says my oldValue is nil (in didSet{}). I don't know how to fix it.

Following is my code:

class MapViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var mapView: MKMapView! {
        didSet {
            mapView.mapType = .Standard
            mapView.delegate = self
        }
    }


    var location:CLLocation? {
        didSet {
            clearWayPoints()
            if location != nil {
                println(location!.coordinate.longitude)
                let coordinate = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
                let pin = MapPin(coordinate: coordinate, title: "Current", subtitle: "here")
                setAnnotation(pin)
            }
        }
    }

    private func clearWayPoints() {
        if mapView?.annotations != nil {
            mapView.removeAnnotations(mapView.annotations as [MKAnnotation])
        }
    }

    func setAnnotation(pin: MKAnnotation) {        
        mapView.addAnnotation(pin)
        mapView.showAnnotations([pin], animated: true)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        var view = mapView.dequeueReusableAnnotationViewWithIdentifier(Constants.AnnotationViewReuseIdentifier)
        if view == nil {
            view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: Constants.AnnotationViewReuseIdentifier)
            view.canShowCallout = true
        } else {
            view.annotation = annotation
        }
        return view

    }

    struct Constants {
        static let AnnotationViewReuseIdentifier = "map cell"
    }
}

My model is simply the var location:CLLocation?, and I update this value from my segue.

I'm sure I get the correct coordinate in the println().

But Xcode always says

fatal error: unexpectedly found nil while unwrapping an Optional value

And I found the nil seems to be oldValue=(CLLocation?)nil!

Following is my simple class MapPin which implements MKAnnotation

class MapPin: NSObject, MKAnnotation {
    var coordinate: CLLocationCoordinate2D
    var title: String?
    var subtitle: String?

    init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String) {
        self.coordinate = coordinate
        self.title = title
        self.subtitle = subtitle
    }
}

Upvotes: 2

Views: 1760

Answers (1)

chillinger
chillinger

Reputation: 450

I just had the exact same problem and fixed it! You're trying to add an annotation to a mapView that is not initialized yet.

The problem is that you're setting the location in prepareForSegue:. At this point your mapView is nil. Call mapView.addAnnotation: in viewDidLoad: to fix it.

I guess that's why it says "Do any additional setup after loading the view." because then all your outlets are initialized.

Hope this helps!

Upvotes: 5

Related Questions