Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKAnnotation and setCoordinate:

I have a custom class that conforms to the MKAnnotation protocol. An instance of this class is held by a viewController, along with an object of type MKMapView, called _mapView. I've set the viewController to be _mapView's delegate. In the custom class interface I've declared:

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

as it's required per the MKAnnotation protocol. I also @synthesize it in that same class's implementation. However when I send the following message from the viewController:

[_mapView addAnnotation:myCustomClass];

I get an error:

<NSInvalidArgumentException> -[myCustomClass setCoordinate:]: unrecognized selector sent to instance 0x1479e0

If I go into my custom Class's implementation file and define

- (void) setCoordinate:(CLLocationCoordinate2D)newCoordinate
{
    coordinate = newCoordinate;
}

then the annotation is added to the map successfully.

Shouldn't @synthesize coordinate; take care of the setCoordinate: method? It seems odd that I have to both @synthesize the coordinate as well as write the (void) setCoordinate: method.

What am I missing?

like image 436
Nick Avatar asked Aug 19 '12 02:08

Nick


1 Answers

When you say

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

the "readonly" is telling the compiler to only create a getter method for your property, not a setter method.

Since CLLocationCoordinate2D is a C struct and not an object, you can use:

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

if you want the compiler to automatically create both the getter and setter.

like image 157
jonkroll Avatar answered Nov 02 '22 22:11

jonkroll