无法识别的选择器发送到实例?

时间:2013-05-30 18:44:47

标签: ios objective-c mapkit

如何解决此类错误: 无法识别的选择器发送到实例?

CLLocationCoordinate2D userCoordinate = userLocation.location.coordinate;
CGFloat latDelta = rand()*.035/RAND_MAX -.02;
CGFloat longDelta = rand()*.03/RAND_MAX -.015;
CLLocationCoordinate2D newCoord = { userCoordinate.latitude + latDelta, userCoordinate.longitude + longDelta };
myPoint *mp = [[myPoint alloc] initWithCoordinate:newCoord title:[NSString stringWithFormat:@"Vitaliy Home %d",i] subTitle:@"Home Sweet Home"];
[mv addAnnotation:mp]; //mv - (MKMapView *)mv

错误在线:

myPoint *mp = [[myPoint alloc] initWithCoordinate:newCoord title:[NSString stringWithFormat:@"Vitaliy Home %d",i] subTitle:@"Home Sweet Home"];

class myPoint:

@interface myPoint : NSObject<MKAnnotation> {
    NSString *title;
    NSString *subTitle;
    CLLocationCoordinate2D coordinate;
}

@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *subTitle;

- (id)initWithCoordinate:(CLLocationCoordinate2D) c title:(NSString *) t subTitle:(NSString *) st;

@end

谢谢。

2 个答案:

答案 0 :(得分:2)

你可能在initWithCoordinate:...方法中做了类似的事情:

self.coordinate = c;

这不是一个简单的赋值,这是调用一个不存在的方法(属性设置器setCoordinate:),它以异常结束。

修复编译器警告!

答案 1 :(得分:0)

问题在于方法的执行不正确,谢谢@BrunoKoga。