MKAnnotation和setCoordinate:

Nic*_*ick 7 cocoa-touch mkannotation

我有一个符合MKAnnotation协议的自定义类.此类的一个实例由一个viewController和一个名为MKMapView的对象控制_mapView.我已将viewController设置为_mapView的委托.在我自己声明的自定义类接口中:

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
Run Code Online (Sandbox Code Playgroud)

因为它符合MKAnnotation协议的要求.我也在@synthesize同一个类的实现中.但是当我从viewController发送以下消息时:

[_mapView addAnnotation:myCustomClass];
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

<NSInvalidArgumentException> -[myCustomClass setCoordinate:]: unrecognized selector sent to instance 0x1479e0
Run Code Online (Sandbox Code Playgroud)

如果我进入我的自定义类的实现文件并定义

- (void) setCoordinate:(CLLocationCoordinate2D)newCoordinate
{
    coordinate = newCoordinate;
}
Run Code Online (Sandbox Code Playgroud)

然后注释成功添加到地图中.

@synthesize coordinate;应该照顾的setCoordinate:方法?奇怪的是,我既有@synthesize坐标也有写(void) setCoordinate:方法.

我错过了什么?

jon*_*oll 14

当你说

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
Run Code Online (Sandbox Code Playgroud)

"readonly"告诉编译器只为你的属性创建一个getter方法,而不是setter方法.

由于CLLocationCoordinate2D是C结构而不是对象,因此您可以使用:

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
Run Code Online (Sandbox Code Playgroud)

如果您希望编译器自动创建getter和setter.