0

I am setting my map view to delegate yet so I can place an annotation callout on my pin, but I have a geocode that does something else only when a button is clicked. When I set the map view to delegate and I try to place a pin it crashes. How can I fix this?

func action(gestureRecognizer:UIGestureRecognizer) {
    let touchPoint = gestureRecognizer.locationInView(self.mapView)
    let newCoord:CLLocationCoordinate2D = mapView.convertPoint(touchPoint, toCoordinateFromView: self.mapView)

    //var newAnotation = MKPointAnnotation()

    //
    self.mapView.delegate = self
    //

    self.newAnotation = MKPointAnnotation()
    self.newAnotation.coordinate = newCoord
    newAnotation.title = "New Location"
    newAnotation.subtitle = "New Subtitle"
    //mapView.addAnnotation(newAnotation)

    self.pinAnnotationView = MKPinAnnotationView(annotation: self.newAnotation, reuseIdentifier: nil)

    self.mapView.centerCoordinate = self.newAnotation.coordinate
    self.mapView.addAnnotation(self.pinAnnotationView.annotation!)
}

Screenshot of where it crashes,

enter image description here

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
BOB
  • 105
  • 1
  • 7

1 Answers1

0

You must be getting the following error log in your console output.And thats because you have not initialise your geoCoder instance.

fatal error: unexpectedly found nil while unwrapping an Optional value

Declare it like this

var geoCoder: CLGeocoder = CLGeocoder()
Muneeba
  • 1,756
  • 10
  • 11