返回nil时,viewForAnnotation抛出错误

时间:2012-11-19 03:17:31

标签: objective-c ios ios6

所以我已经实现了我的viewForAnnotation

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
static NSString *identifier = @"id";

if ([annotation isKindOfClass:[infl8Node class]]) {
    NSLog(@"Creating a pin");
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    } else {
        annotationView.annotation = annotation;
    }

    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;

    return annotationView;
}

return nil;
}

但是我仍然遇到这个错误:

2012-11-18 22:12:35.608 Infl8[5960:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[infl8Node coordinate]: unrecognized selector sent to instance 0x846f460'

有谁知道可能导致此错误的原因?我已将其跟踪到注释属于MKUserLocation类的情况。

编辑: 这是infl8Node.h文件:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface infl8Node : NSObject <MKAnnotation>

@property (nonatomic, strong) NSString *accountName;
@property (nonatomic, strong) NSString *status;
@property (nonatomic, strong) CLLocation *location;
@property (nonatomic, strong) NSDictionary *address;

- (id) initWithDictionary:(NSDictionary *)dict;
- (MKMapItem *) mapItem;
@end

1 个答案:

答案 0 :(得分:0)

实现MKAnnotation协议的类必须实现coordinate属性。

infl8Node类似乎没有这样做,这就是您收到unrecognized selector错误的原因。

该课程至少需要实现coordinate方法,该方法返回与其位置相关联的坐标。