Swift 错误:无法将NSKVONotifying_MKUserLocation类型的值强制转换为Park_View.AttractionAnnotation
使用此函数时:Swift 错误:无法将NSKVONotifying_MKUserLocation类型的值强制转换为Park_View.AttractionAnnotation,swift,mapkit,core-location,mkannotation,Swift,Mapkit,Core Location,Mkannotation,使用此函数时: func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { let annotationView = AttractionAnnotationView(annotation: annotation, reuseIdentifier: "Attraction") annotationView.canShowCallou
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
let annotationView = AttractionAnnotationView(annotation: annotation, reuseIdentifier: "Attraction")
annotationView.canShowCallout = true
return annotationView
}
发生以下错误:
无法将“NSKVONotifying_MKUserLocation”(0x7e8a62b0)类型的值强制转换为“Park_View.AttractionAnnotation”(0xf7948)
它工作正常,但当我尝试添加CoreLocation以在代码中查找用户位置时,我开始出现此错误。函数“AttractionAnnotationView:”可能返回MKUserLocation对象,而不是“AttractionAnnotation”对象。我发现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
}
}
在AttractionAnnotation类中是否有自定义重写的
isEqual()
函数?如果是,请在比较之前检查它是否未将比较对象(函数参数)强制转换为AttractionAnnotation 当您单击/点击用户位置时,在Xcode 10.2中也会发生同样的事情。。。因此,请尝试以下方法:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if (view.annotation is MKUserLocation) {
return
}
...
}