2011-07-17 55 views
1

我想在我的谷歌地圖查看添加PIN /地址標註。當我這樣做everyting工作正常:MapKit崩潰 - 無法識別的選擇發送到實例

[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]; 

的應用chrases somethimes當引腳應該在我看來去somethimes此錯誤:

'NSInvalidArgumentException' 的,理由是: ' - [NSCFNumber長]:無法識別的選擇發送到實例0x5851c80'

回答

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; 
} 

您直接使用實例變量的類插件設置值而不是屬性,所以您傳遞的實際值不會被複制,因此不會被保留。

+0

日Thnx,這樣的作品,除了self.coordinate。那個人需要保持自我。否則會給出錯誤。你能再試一次解釋爲什麼我需要這樣做,說實話我沒有明白。是因爲座標不是指針,標題和副標題是? – Melvin

+1

號當您在.h文件創建一個@property,並在以後使用@合成你居然讓編譯器來構建setter和getter功能爲您的實例變量。當你使用self.variableName就好像調用'[self setVariableName:newValue]'在你的情況下會做'[variableName release]; variableName = [newValue copy];'。希望這是有道理的。如果沒有,你可以嘗試閱讀[this](http://www.raywenderlich.com/2712/using-properties-in-objective-c-tutorial)。 –

+0

@ user706933 @Mihai Fratu記住,[蘋果阻止在初始化訪問方法的使用(http://developer.apple.com/library/mac/documentation/cocoa/conceptual/MemoryMgmt/Articles/mmPractical.html# // apple_ref/DOC/UID/TP40004447-SW6)。 – albertamg

0

NSCFNumber是NSNumber的私人實現之一。 NSNumber沒有稱爲長度的方法,所以它不能識別選擇器。到現在爲止還挺好。

這是很難說使用這個NSNumber的是哪裏。 ISTM認爲問題不在您的代碼中。我不知道MapKit足夠給你一個解決方法。

相關問題