0

I have a MKMapView. In this view I create some annotations. When the user clicks on the annotation disclosure button, I want to push to the detail view.

When I create the annotation, I also fill a NSMutableDictionary to be able to grab the store object afterwards with the point as key.

MKPointAnnotation *point = [[MKPointAnnotation alloc] init];

CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = [[store storeLatitude] floatValue];
annotationCoord.longitude = [[store storeLongitude] floatValue];

point.coordinate = annotationCoord;

NSString *title = [store storeName];
point.title = title;
point.subtitle = location;

// keep reference to store object with the annotation point as key
[annotationStoreDictionary setObject:store forKey:point];

When the user selects the annotation, I want to call the DetailViewController and pass the object:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

    DetailViewController *con = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
    Store *store = [annotationStoreDictionary objectForKey:view.annotation];
    con.store = store;

    [[self navigationController] pushViewController:con animated:YES];
}

The problem is that MKPointAnnotation is not compliant with the NSCoping protocol. It says:

Sending 'MKPointAnnotation *__strong' to parameter of incompatible type 'id<NSCoping>'

How can this be fixed?

nimrod
  • 5,595
  • 29
  • 85
  • 149
  • 2
    As @Jesper suggests, subclass MKPointAnnotation and add custom properties to keep a reference to each annotation's Store in the annotation object itself. The separate dictionary is not necessary. Another approach is this: Since you already have a `Store` class, simply make it implement the `MKAnnotation` protocol and add the Store objects themselves to the map. This way, the annotations _are_ the Stores. To create a class that implements MKAnnotation, see http://stackoverflow.com/questions/5939223/store-data-in-mkannotation for one example. –  Oct 28 '14 at 14:03
  • +1, @Anna . Implementing `MKAnnotation` is a great alternative. – Jesper Oct 28 '14 at 14:05
  • 1
    @Anna, if you provide this as an answer, I will accept it! Thanks for the input, it worked. – nimrod Oct 29 '14 at 14:48

1 Answers1

1

You can't use something that doesn't conform to the NSCopying protocol as a key in a dictionary. You'll have to use something else that does.

One way of getting around this would be to make a subclass of MKPointAnnotation with extra properties for correlation - and in that case, you may as well also just use the extra property or properties on that object and not go through the dictionary in the first place.

Jesper
  • 7,477
  • 4
  • 40
  • 57