2015-06-26 30 views
2

在使用本功能:錯誤:無法投類型NSKVONotifying_MKUserLocation的價值Park_View.AttractionAnnotation

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

此錯誤發生:

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

它運作良好,但是當我嘗試添加CoreLocation找到我的代碼的用戶位置我開始有這個錯誤。

回答

12

我發現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 
    } 
} 
0

函數「AttractionAnnotationView:」可能返回MKUserLocation對象而不是「AttractionAnnotation」對象。

0

您的AttractionAnnotation類中是否有自定義覆蓋的isEqual()函數?如果是,請在比較之前檢查它是否將比較對象(函數參數)轉換爲AttractionAnnotation。

相關問題