我正在尝试访问由rightCalloutAccessoryView触发时在我的自定义注释数据中设置的一些自定义数据。我收到编译器错误,如下所述。这是我的自定义MKAnnotation,带有几个额外的变量 - status& supplierdataindex
class CustomMapPinAnnotation : NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String
var subtitle: String
var status: Int
var supplierdataindex: Int
init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String, status: Int, supplierdataindex: Int) {
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
self.status = status
self.supplierdataindex = supplierdataindex
}
} // CustomMapPinAnnotation
var myCustomMapPinAnnotationArray = [CustomMapPinAnnotation] ()
// I build an array and put that into myCustomMapPinAnnotationArray
...
// I add the annotations initially in viewDidLoad in the ViewController.swift via this call
theMapView.addAnnotations(myCustomMapPinAnnotationArray)
在地图和它的引脚方面一切都很好但现在我想访问myCustomMapPinAnnotation数组的自定义部分,特别是“status”和“supplierdataindex” 这将决定更详细的观点。我正在使用calloutAccessoryControlTapped来捕获点击。
问题是这个编译器给我一个错误,我试图设置访问权限,我认为这会让我指向应该已经内置到我认为的注释数据中的自定义数据。
'MKAnnotation'无法转换为'CustomMapPinAnnotation'
func mapView(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == annotationView.rightCalloutAccessoryView {
// This works and prints the data
println("Right Callout was pressed and title = \(annotationView.annotation.title)")
// This line yields a compiler error of: 'MKAnnotation is not convertible to 'CustomMapPinAnnotation'
var pinannotation : CustomMapPinAnnotation = annotationView.annotation
println("status was = \(myannotation.status)")
println("supplierdataindex was = \(myannotation.supplierdataindex)")
}
}
答案 0 :(得分:2)
您必须将其类型转换为CustomMapPinAnnotation,
func mapView(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == annotationView.rightCalloutAccessoryView {
if let pinannotation = annotationView.annotation as? CustomMapPinAnnotation{
println("status was = \(pinannotation.status)")
println("supplierdataindex was = \(pinannotation.supplierdataindex)")
}
}
}