MKnnotation and setCoordinate:

I have a custom class that conforms to the MKAnnotation protocol. An instance of this class belongs to the viewController along with an object of type MKMapView called _mapView . I set viewController as a _mapView delegate. In the user class interface, I stated:

 @property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

as required by the MKAnnotation protocol. I also @synthesize in the same class implementation. However, when I send the following message from viewController:

 [_mapView addAnnotation:myCustomClass]; 

I get an error message:

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

If I go to my class implementation file and define

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

then the annotation is added to the map successfully.

Should not @synthesize coordinate; take care of the setCoordinate: method setCoordinate: It seems strange that I should both @synthesize coordinate and write the (void) setCoordinate: method.

What am I missing?

+7
source share
1 answer

When you speak

 @property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

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

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

 @property (nonatomic, assign) CLLocationCoordinate2D coordinate; 

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

+14
source

All Articles