1

MKPointAnnotation 型 (および mapview 関数の内部にある) の注釈の名前で変数を の型にキャストしようとしています。CustomPointAnnotationこれは、 から拡張する作成したクラスですMKPointAnnotation

これを行うコード行は次のとおりです。

let ca = annotation as! CustomPointAnnotation

CustomPointAnnotation の実装は次のとおりです。

class CustomPointAnnotation: MKPointAnnotation {
    var image = UIImage()
}

しかし、これを行うと、ここに示す最初の行 (let ca = ....) で実行時エラーが発生します。

"Could not cast value of type 'NSKVONotifying_MKPointAnnotation' (0x7ff93d91fea0) to 'ParseStarterProject_Swift.CustomPointAnnotation' (0x1056c1f00)."

助けていただければ幸いです、ありがとう。

エラーが発生する行の ここに画像の説明を入力 画像: コンソールのエラーの画像: ここに画像の説明を入力

これは、エラーが発生する mapview メソッド全体です。アノテーションは関数によって MKAnnotation として定義されます。

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    let identifier = "MyCustomAnnotation"
    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView?.canShowCallout = true

        annotationView!.image = UIImage(named: "RaceCarMan3.png")

    } else {
        annotationView!.annotation = annotation
    }

    let ca = annotation as! CustomPointAnnotation

    configureDetailView(annotationView!, carImage: ca.imageName)

    return annotationView
}

func configureDetailView(annotationView: MKAnnotationView, carImage: UIImage) {
    annotationView.detailCalloutAccessoryView = UIImageView(image: carImage)
}
4

0 に答える 0