【发布时间】:2016-10-30 23:44:32
【问题描述】:
我有一个 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
【问题讨论】:
标签: swift generics protocols mkannotation