2

I have created an application that contains a mapView. I added a couple of annotations around my location. I want when I click on an annotation to show me the title in a label.

I did that and I took the title and put it in myLabel but when I'm clicking on my location, the application stops working and I get the error:

(Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value)

Here is my code:

import UIKit
import MapKit
import CoreLocation
import Foundation
class ViewControllermap: UIViewController, MKMapViewDelegate {
    @IBOutlet weak var mymap: MKMapView!
    @IBOutlet weak var viewaddress: UILabel!
    var selectedAnnotation: MKPointAnnotation?
    var locationManager: CLLocationManager = CLLocationManager()
    let regionInMeters:Double = 3000
    var previouslocation: CLLocation?
    var longitudeLocation:String = ""
    var latituseLocation:String  = ""
    override func viewDidLoad() {

        mymap.delegate = self
      //  checkLocationSevices()
        super.viewDidLoad()
        if let location = locationManager.location?.coordinate
               {
                   let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
                   mymap.setRegion(region, animated: true)
               }
        mymap.showsUserLocation = true
        let location = CLLocationCoordinate2DMake(35.1623, 33.3178)
        let annotation = MKPointAnnotation()
        annotation.coordinate = location
        annotation.title = "P1"
        annotation.subtitle = "28 oct street"
        mymap.addAnnotation(annotation)


        let location2 = CLLocationCoordinate2DMake(35.1658, 33.3147)
        let annotation2 = MKPointAnnotation()
        annotation2.coordinate = location2
        annotation2.title = "P2"
        annotation2.subtitle = " Makedonitissis 46, Nicosia 2417"
        mymap.addAnnotation(annotation2)

        let location3 = CLLocationCoordinate2DMake(35.1600, 33.3770)
        let annotation3 = MKPointAnnotation()
        annotation3.coordinate = location3
        annotation3.title = "P3"
        annotation3.subtitle = " Kallipoleos 75, Nicosia 1678"
        mymap.addAnnotation(annotation3)

        let location4 = CLLocationCoordinate2DMake(35.1602, 33.3390)
        let annotation4 = MKPointAnnotation()
        annotation4.coordinate = location4
        annotation4.title = "P4"
        annotation4.subtitle = "Diogenis Str 6 Nicosia CY, 2404"
        mymap.addAnnotation(annotation4)

        let location5 = CLLocationCoordinate2DMake(35.1673, 33.3277)
        let annotation5 = MKPointAnnotation()
        annotation5.coordinate = location5
        annotation5.title = "P5"
        annotation5.subtitle = "Neas Egkomis 4, Egkomi"
        mymap.addAnnotation(annotation5)

        let location6 = CLLocationCoordinate2DMake(35.1680, 33.3375)
        let annotation6 = MKPointAnnotation()
        annotation6.coordinate = location6
        annotation6.title = "P6"
        annotation6.subtitle = "Neas Egkomis 4, Egkomi"
        mymap.addAnnotation(annotation6)

        let location7 = CLLocationCoordinate2DMake(35.1460, 33.3357)
        let annotation7 = MKPointAnnotation()
        annotation7.coordinate = location7
        annotation7.title = "P7"
        annotation7.subtitle = "Neas Egkomis 4, Egkomi"
        mymap.addAnnotation(annotation7)

        let location8 = CLLocationCoordinate2DMake(35.1580, 33.3275)
        let annotation8 = MKPointAnnotation()
        annotation8.coordinate = location8
        annotation8.title = "P8"
        annotation8.subtitle = "Neas Egkomis 4, Egkomi"
        mymap.addAnnotation(annotation8)



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

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        if let annotationCoordinate = view.annotation?.coordinate {
        self.selectedAnnotation = view.annotation as? MKPointAnnotation

        viewaddress.text = "\(selectedAnnotation!.subtitle!)" as String
        }

    }
}

enter image description here

This error happens just when I click on my location and code is working for other annotations.

Anyone know how can I fix this?

Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
jeena azeez
  • 388
  • 1
  • 3
  • 12
  • Possible duplicate of: [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) – TylerP May 03 '20 at 22:51

1 Answers1

0

Your selectedAnotation or its subtitle is nil .. thats why you are getting this crash ... use optional chaining or optional binding to avoid this .. Roughly something like this ... // not tested code

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if let annotationCoordinate = view.annotation?.coordinate {
    self.selectedAnnotation = view.annotation as? MKPointAnnotation

      if let annnotation = selectedAnnotation , let subTitle = annnotation.subTitle as? String {

           viewaddress.text = "\(subTitle)" 
      }
    }

}
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49