这个错误只出现在 Xcode Version 5.0
在我使用 Xcode 4.6.2 版 创建我的应用程序之前,它对我来说运行良好,但我在 Xcode 5.0 版 中遇到了这个错误
我创建了自定义注释类来生成我更新的位置和地址。
我的代码是:
注释 View .h
#import <MapKit/MapKit.h>
@interface AnnotationView : MKPlacemark
@property (nonatomic, readwrite, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *subtitle;
@end
注释 View .m
#import "AnnotationView.h"
@implementation AnnotationView
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary *)addressDictionary
{
if ((self = [super initWithCoordinate:coordinate addressDictionary:addressDictionary]))
{
self.coordinate = coordinate;
}
return self;
}
@end
上面是我的自定义类。我在我的 MapView 中使用了它。
CLLocationCoordinate2D theCoordinate ;
theCoordinate.latitude = [self.latitude doubleValue];
theCoordinate.longitude = [self.longitude doubleValue];
AnnotationView *annotation = [[AnnotationView alloc] initWithCoordinate:theCoordinate addressDictionary:nil] ;
annotation.title = self.businessName;
annotation.subtitle = self.businessAddress;
[self.mapView addAnnotation:annotation];
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:MKCoordinateRegionMakeWithDistance(theCoordinate, 8000, 8000)];
[self.mapView setRegion:adjustedRegion animated:YES];
请指出我哪里出错了。
最佳答案
AnnotationView
的父类(super class),即 MKPlacemark
,已经存储了 coordinate
- 请注意,您将它传递给 super initWithCoordinate:
方法 - 因此您无需在子类中存储 坐标
。让父类(super class)处理它。
换句话说,您应该从您的 AnnotationView
类中删除这一行:
self.coordinate = coordinate;
如果您需要从 AnnotationView
访问 coordinate
属性,只需使用 [super coordinate]
。
使用您自己的同名属性覆盖父类(super class)的属性时要小心——一般来说,您不想这样做!
至于为什么您在 Xcode 5 中遇到问题,而之前一切正常:这可能是因为不同版本的编译器对代码的解释略有不同。您的代码总是有问题,只是编译器现在注意到它有问题。
关于iphone - 异常 'NSInvalidArgumentException' ,原因 : '-[AnnotationView setCoordinate:]: unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19315605/