Kokanee
Kokanee

Reputation: 975

Problems accessing custom MKAnnotation data via rightCalloutAccessoryView

I am trying to access some custom data I've setup in my custom annotation data when triggered by the rightCalloutAccessoryView. I am getting a compiler error as described below. Here's my custom MKAnnotation with a couple of extra variables - 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)

Everything works great in terms of the map and it's pins but now I want to access the custom parts of that myCustomMapPinAnnotation array, specifically the "status" and and "supplierdataindex" which will drive decisions for more detailed views. I am using the calloutAccessoryControlTapped to catch the click.

The problem is this compiler gives me an error below where I try to setup access that I thought would get me pointed to the custom data that should have been built into the annotation data I thought.

'MKAnnotation" is not convertible to '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)")     
    }
}

Upvotes: 0

Views: 1442

Answers (1)

Sandeep
Sandeep

Reputation: 21154

You will have to typecast it to CustomMapPinAnnotation like this,

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

       }
    }
}

Upvotes: 2

Related Questions