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

时间:2015-06-26 09:38:23

标签: swift mapkit core-location mkannotation

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.

4 个答案:

答案 0 :(得分:16)

我发现MKUserLocation也是一个注释。

这是我出来的解决方案,它解决了错误。

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

答案 1 :(得分:0)

可能是函数" AttractionAnnotationView:"返回MKUserLocation对象而不是' AttractionAnnotation'对象

答案 2 :(得分:0)

您的AttractionAnnotation类中是否有自定义重写的isEqual()函数?如果是这样,请在比较之前检查它是否未将比较对象(函数参数)强制转换为AttractionAnnotation。

答案 3 :(得分:0)

当您单击/轻按用户位置时,Xcode 10.2中也会发生相同的事情,因此请尝试以下操作:

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

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