我有一个MapViewController用于在地图上展示标注。它包含一个类型为MapPresentable的对象。
protocol MapPresentable {
associatedtype AnnotationElement: MKAnnotation
var annotations: [AnnotationElement] { get }
}
class MapViewController<M: MapPresentable>: UIViewController {
var mapPresentable: M!
}
如果mapPresentable
符合RoutePresentable协议,MapViewController也可以在地图上呈现路线。
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