Saya telah membuat aplikasi yang berisi mapView. Saya menambahkan beberapa anotasi di sekitar lokasi saya. Saya ingin ketika saya mengklik anotasi untuk menunjukkan judul dalam label.

Saya melakukan itu dan saya mengambil judul dan meletakkannya di myLabel tetapi ketika saya mengklik lokasi saya, aplikasi berhenti bekerja dan saya mendapatkan kesalahan:

(Utas 1: Kesalahan fatal: Tanpa diduga ditemukan nihil saat membuka bungkus nilai Opsional)

Ini kode saya:

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

Kesalahan ini terjadi tepat ketika saya mengklik lokasi saya dan kode berfungsi untuk anotasi lainnya.

Adakah yang tahu bagaimana saya bisa memperbaikinya?

2
jeena azeez 4 Mei 2020, 01:44

1 menjawab

Jawaban Terbaik

Anotasi yang Anda pilih atau subjudulnya adalah nihil .. itu sebabnya Anda mendapatkan crash ini ... gunakan rantai opsional atau pengikatan opsional untuk menghindari ini .. Kira-kira seperti ini ... // kode tidak diuji

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)" 
      }
    }

}
0
Jawad Ali 3 Mei 2020, 23:11