MapKit崩溃 - 发送到实例的无法识别的选择器

时间:2011-07-17 15:46:59

标签: iphone objective-c ipad

我正在尝试在Google地图视图中添加引脚/地址注释。当我这样做时,每一件工作都很好:

[self addPin: CLLocationCoordinate2DMake(lat, lng) title: @"test" subtitle: @"test"];

使用:

- (void) addPin: (CLLocationCoordinate2D) position title: (NSString *) pinTitle subtitle: (NSString *) pinSubtitle{

AddressAnnotation *addAnnotation = [[AddressAnnotation alloc] initWithTitle:pinTitle andCoordinate:position andSubtitle:pinSubtitle];

[mapView addAnnotation:addAnnotation];
[addAnnotation autorelease];

}

#import "AddressAnnotation.h"

@implementation AddressAnnotation

@synthesize title, coordinate, subtitle;

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d andSubtitle:(NSString *)sbtitle {
[super init];
title = ttl;
coordinate = c2d;
subtitle = sbtitle;
return self;
}

- (void)dealloc {
[title release];
[super dealloc];
}

@end

使用:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface AddressAnnotation : NSObject <MKAnnotation> {

NSString *title;
CLLocationCoordinate2D coordinate;
NSString *subtitle;
}

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d andSubtitle:(NSString *)sbtitle;

@end
当我这样做时出现问题:

NSString *detailTitle   = [NSString stringWithFormat:@"%@", [key objectForKey:@"name"]];
NSString *detailSubtitle    = [NSString stringWithFormat:@"%@", [key objectForKey:@"vicinity"]];

 NSLog(@"%@", detailSubtitle);
[self addPin: CLLocationCoordinate2DMake(lat, lng) title: detailTitle subtitle: detailSubtitle];

应用程序会在我的视图中显示引脚并且有时会出现此错误时选择一些时间:

'NSInvalidArgumentException',原因:' - [NSCFNumber length]:无法识别的选择器发送到实例0x5851c80'

2 个答案:

答案 0 :(得分:2)

尝试将init功能从AddressAnnotation.m更改为

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d andSubtitle:(NSString *)sbtitle {
    self = [super init];
    if (self) {
        self.title = ttl;
        self.coordinate = c2d;
        self.subtitle = sbtitle;
    }
    return self;
}

您正在直接使用实例变量来设置类实例中的值而不是属性,因此您传递的实际值不会被复制,因此不会被保留。

答案 1 :(得分:0)

NSCFNumber是NSNumber的私有实现之一。 NSNumber没有名为length的方法,因此它无法识别选择器。到现在为止还挺好。

很难说这个NSNumber的使用位置。 ISTM问题不在您的代码中。我不太了解MapKit给你一个解决方法。