Unrecognized selector sent to instance?

半世苍凉 提交于 2020-01-25 07:58:07

问题


How to fix such an error: unrecognized selector sent to instance ?

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

Error is on line :

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

Thank you.


回答1:


You are probably doing something like this in the initWithCoordinate:... method:

self.coordinate = c;

This is not a simple assignment, this is calling a method (property setter setCoordinate:) which is not there and it ends in exception.

Fix compiler warnings!




回答2:


The problem was in not correct implementation of the method, thanks @BrunoKoga.



来源:https://stackoverflow.com/questions/16843675/unrecognized-selector-sent-to-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!