我有一个符合MKAnnotation协议的自定义类。该类的一个实例由一个viewController以及一个名为_mapView的MKMapView类型的对象持有。我已经将viewController设置为_mapView的委托。在我声明的自定义类接口中:
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;因为它是每个MKAnnotation协议所要求的。我还在同一个类的实现中@synthesize它。但是,当我从viewController发送以下消息时:
[_mapView addAnnotation:myCustomClass];我得到一个错误:
<NSInvalidArgumentException> -[myCustomClass setCoordinate:]: unrecognized selector sent to instance 0x1479e0如果我进入自定义类实现文件并定义
- (void) setCoordinate:(CLLocationCoordinate2D)newCoordinate
{
coordinate = newCoordinate;
}则注解被成功添加到地图中。
@synthesize coordinate;不应该处理setCoordinate:方法吗?似乎很奇怪,我必须既要@synthesize坐标,又要编写(void) setCoordinate:方法。
我遗漏了什么?
发布于 2012-08-19 10:58:09
当你说
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;"readonly“是告诉编译器只为你的属性创建getter方法,而不是setter方法。
因为CLLocationCoordinate2D是一个C结构而不是一个对象,所以你可以使用:
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;如果您希望编译器自动创建getter和setter。
https://stackoverflow.com/questions/12023766
复制相似问题