iphone - 异常 'NSInvalidArgumentException' ,原因 : '-[AnnotationView setCoordinate:]: unrecognized selector sent to instance

标签 iphone ios mapkit

这个错误只出现在 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/

相关文章:

ios - 如何像应用程序商店安装按钮 Action 一样产生小振动?

ios - Swift MKMapView KVO

ios - 自定义 Apple map 注释无法正常工作

ios - UITableView 以编程方式滚动到元素

xcode - MapKit 在 XCode 4.6 中滚动 map 时更改注释 View 顺序

iphone - 如何制作具有左推效果的自定义转场?

iphone - 检查 NSFetchedResultsController/Core Data 中的现有值

javascript - Google map 的标记在 iPhone/iPad 上不显示 InfoBubble

iphone - 获取GKLocalPlayer上面两个好友的分数

ios - 运行时的单元格高度与 Storyboard XCode9 中的单元格高度不同