2014-09-22 37 views
0

我试图访问我在自定义注释数据中由rightCalloutAccessoryView触发时设置的一些自定义数据。我收到了一个编译器错误,如下所述。这里是我的一对夫妇的其他变量的定制MKAnnotation - 状态& supplierdataindex通过rightCalloutAccessoryView访问自定义MKAnnotation数据时遇到的问题

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阵列的定制部件,特别是“状态“和”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)")  
    } 
} 

回答

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)")  

     } 
    } 
} 
+0

韩国社交协会......正是我需要让我过来这个。 – Kokanee 2014-09-22 16:19:59

相关问题