Error: Could not assign value of type NSKVONotifying_MKUserLocation to Park_View.AttractionAnnotation

When using this function:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    let annotationView = AttractionAnnotationView(annotation: annotation, reuseIdentifier: "Attraction")
    annotationView.canShowCallout = true
    return annotationView
}

      

This error happened:

Failed to pass value of type NSKVONotifying_MKUserLocation (0x7e8a62b0) to value "Park_View.AttractionAnnotation" (0xf7948).

It works well, but when I try to add CoreLocation to find the user location for my code, I start with this error.

+3


source to share


4 answers


I found out that MKUserLocation is annotation too.

Here is the solution I came out with and it resolves the error.



func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if (annotation is MKUserLocation) {
        return nil
    }
    else {
        let annotationView = AttractionAnnotationView(annotation: annotation, reuseIdentifier: "Attraction")
        annotationView.canShowCallout = true
        return annotationView
    }
}

      

+15


source


perhaps the AttractionAnnotationView: function returns a MKUserLocation object instead of a AttractionAnnotation object.



0


source


Do you have a custom overridden function isEqual()

in your AttractionAnnotation class? If so, make sure it does not distinguish the object being compared (function parameter) from AttractionAnnotation before comparing.

0


source


The same thing happens in Xcode 10.2 when you click / tap on the user's location ... so try something like this:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

    if (view.annotation is MKUserLocation) {
        return
    }
    ...
}

      

0


source







All Articles