簡體   English   中英

異常'NSInvalidArgumentException',原因:'-[AnnotationView setCoordinate:]:無法識別的選擇器已發送到實例

[英]exception 'NSInvalidArgumentException', reason: '-[AnnotationView setCoordinate:]: unrecognized selector sent to instance

僅在Xcode 版本5.0中出現此錯誤

在Xcode 版本4.6.2中創建此應用之前,它對我來說運行良好,但是在Xcode 版本5.0中出現此錯誤。

我創建了自定義注釋類,以生成更新的位置和地址。

我的代碼是:

AnnotationView.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

AnnotationView.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的超類(即MKPlacemark )已經存儲了coordinate -請注意,您已將其傳遞到super initWithCoordinate:方法中-因此您無需將coordinate存儲在子類中。 讓超類處理它。

換句話說,您應該從AnnotationView類中刪除以下行:

self.coordinate = coordinate;

如果您需要從AnnotationView訪問coordinate屬性,只需使用[super coordinate]

請注意,使用您自己的具有相同名稱的屬性覆蓋超類的屬性-一般而言,您不希望這樣做!

至於為什么在以前沒問題的情況下在Xcode 5中遇到問題:這可能是因為不同版本的編譯器對代碼的解釋略有不同。 您的代碼總是有問題的,只是編譯器現在注意到了問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM