Swift 4 添加注释错误在解包时意外发现 nil

Swift 4 adding annotations error unexpectedly found nil while unwrapping

我只是想在我的地图视图中添加注释,但我将 运行 保留在这个错误 "unexpectedly found nil while unwrapping" 中,我似乎无法解决它或找到适用的解决方案我的情况。有人可以告诉我我哪里出错了。提前致谢。我已经尝试将 var mapView: MKMapView! 更改为 var mapView: MKMapView? 但没有成功

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

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

var mapView: MKMapView!
var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    let location = CLLocationCoordinate2D(latitude: 32.2325, longitude:76.3242)
    let region = MKCoordinateRegion(center: location, span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005))
    self.mapView.setRegion(region, animated: true)

    let pin = customPin(pinTitle: "Honory Dalailama Temple", pinSubTitle: "Dharamshala, Himachal Pradesh, India", location: location)
    self.mapView.addAnnotation(pin)
    self.mapView.delegate = self
}


func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customannotation")
    annotationView.image = UIImage(named:"pin")
    annotationView.canShowCallout = true
    return annotationView
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    print("annotation title == \(String(describing: view.annotation?.title!))")
}

fileprivate func setupLocationManager() {self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() == true {

        if CLLocationManager.authorizationStatus() == .restricted || CLLocationManager.authorizationStatus() == .denied || CLLocationManager.authorizationStatus() == .notDetermined {

            locationManager.requestWhenInUseAuthorization()
        }

        locationManager.desiredAccuracy = 1.0
        locationManager.delegate = self
        locationManager.startUpdatingLocation()

    } else {
        print("PLease turn on location services or GPS")
    }

}

fileprivate func setupMapView() {
    let mView = MKMapView(frame: CGRect(x: 0, y: 20, width: view.frame.width, height: view.frame.height))
    mView.showsUserLocation = true
    view.addSubview(mView)
    mView.showsScale = true
    mView.showsPointsOfInterest = true
    mView.showsTraffic = true
    mView.showsCompass = true
    mView.userTrackingMode = .follow
    self.mapView = mView
}

}

这看起来有问题:

print("annotation title == \(String(describing: view.annotation?.title!))")

如果 annotation 为零,您将使用 ! 强制展开它,这会导致崩溃。

改用:

print("annotation title == \(String(describing: view.annotation?.title))")


也不清楚您是否调用了 setupMapView() 并在调用 viewDidLoad() 之前实际设置了 mapView。在 viewDidLoad() 中添加行:

if mapView == nil { setupMapView() }

在使用 mapView.

之前