我想在我的谷歌地圖查看添加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'
日Thnx,這樣的作品,除了self.coordinate。那個人需要保持自我。否則會給出錯誤。你能再試一次解釋爲什麼我需要這樣做,說實話我沒有明白。是因爲座標不是指針,標題和副標題是? – Melvin
號當您在.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)。 –
@ user706933 @Mihai Fratu記住,[蘋果阻止在初始化訪問方法的使用(http://developer.apple.com/library/mac/documentation/cocoa/conceptual/MemoryMgmt/Articles/mmPractical.html# // apple_ref/DOC/UID/TP40004447-SW6)。 – albertamg