Search code examples
swiftmapkitcore-locationmkannotation

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


When using this func:

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

This error occured:

Could not cast value of type 'NSKVONotifying_MKUserLocation' (0x7e8a62b0) to 'Park_View.AttractionAnnotation' (0xf7948).

It is working well but when I try to add CoreLocation to find the user location to my code I start having this error.


Solution

  • I found out that MKUserLocation is an annotation too.

    Here is the solution that I came out with and it solve 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
        }
    }