マップに注釈を表示するためのMapViewControllerがあります。これにはMapPresentable型の オブジェクトが含まれています。プロトコルは一般的な制約としてのみ使用できます
protocol MapPresentable {
associatedtype AnnotationElement: MKAnnotation
var annotations: [AnnotationElement] { get }
}
class MapViewController<M: MapPresentable>: UIViewController {
var mapPresentable: M!
}
MapViewControllerも場合mapPresentable
に地図上に存在するルートはRoutePresentableプロトコルに準拠することができます。
protocol RoutePresentable: MapPresentable {
var getRouteLocations: [CLLocation] { get }
}
しかしMapViewController
if let routePresentable = mapPresentable as? RoutePresentable {
showRoute(routePresentable.getRouteLocations)
}
内部で作られたチェックするとき、私はこのエラーを得た:
Protocol 'RoutePresentable' can only be used as a generic constraint because it has Self or associated type requirements
は、あなたの答えをありがとう!残念ながら、私はあなたにジェネリックソリューションを理解していませんでした。 MapPresentable&RoutePresentableはプロトコルでなければなりません(実際のタイプに準拠しています)。どのように正確にキャストステートメントが動作するのでしょうか?この場合、MapPresentableとRoutePresentableは異なる構造体です。 – GeRyCh
素晴らしい!あなたのアップデートはキャストの問題を解決するのに役立ちました。ありがとうございました! – GeRyCh