我需要创建一个继承自MKAnnotation的协议:
protocol Annotable: MKAnnotation {
...
}
class Annotation: NSObject, Annotable {
var title: String?
var coordinate: CLLocationCoordinate2D
init(title: String, coordinate: CLLocationCoordinate2D) {
self.title = title
self.coordinate = coordinate
}
}当我将这些注释添加到mapView中时,没有问题。
mapView.addAnnotations([Annotation(...), Annotation(...), ...]但是,当我试图在注释集合上循环时,应用程序在运行时崩溃:
for annotation in mapView.annotations { // fatal error: NSArray element failed to match the Swift Array Element type
print(annotation.title)
}我的问题很简单:为什么?
由于注释符合MKAnnotation,所以可以将它们添加到mapView中也就不足为奇了。那我们为什么不能找回它们呢?
非常感谢!
发布于 2016-06-14 20:59:17
您可以通过将协议声明为@objc协议来解决此问题:
@objc protocol Annotable: MKAnnotation {
...
}https://stackoverflow.com/questions/37821400
复制相似问题