plist 파일에서 사용자 정의 주석을로드 할 맵을 표시하는 MapKit을 사용하는 프로그램을 작성하고 있습니다. 각 주석은 제목, 부제, 위도 및 경도와 함께 루트 배열의 사전 항목입니다. 주석을 테스트 목적으로 하드 코딩하면 프로그램이 아름답게 작동합니다. 그러나 MapDemoAnnotation 클래스를 추가하고 속성 목록을 읽으려는 시도로 인해 프로그램이 시작될 때 충돌이 발생합니다.plist에서로드 할 때 MapKit 기반 앱이 충돌합니다
#import "MapDemoAnnotation.h"
@implementation MapDemoAnnotation
@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
-(id)initWithDictionary:(NSDictionary *)dict{
self = [super init];
if(self!=nil){
coordinate.latitude = [[dict objectForKey:@"latitude"] doubleValue];
coordinate.longitude = [[dict objectForKey:@"longitude"] doubleValue];
self.title = [dict objectForKey:@"name"];
self.subtitle = [dict objectForKey:@"desc"];
}
return self;
}
-(void)dealloc{
[title release];
[subtitle release];
[super dealloc];
}
@end
내 RootViewController 클래스에서의 viewDidLoad 방법 같은데요, 그래도 문제입니다 :
여기 내 주석의 구현입니다.
- (void)viewDidLoad {
[super viewDidLoad];
MKMapView *mapView = (MKMapView*)self.view;
mapView.delegate = self;
mapView.mapType=MKMapTypeHybrid;
CLLocationCoordinate2D coordinate;
coordinate.latitude = 39.980283;
coordinate.longitude = -75.157568;
mapView.region = MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000);
//All the previous code worked fine, until I added the following...
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Locations" ofType:@"plist"];
NSData* data = [NSData dataWithContentsOfFile:plistPath];
NSMutableArray* array = [NSPropertyListSerialization propertyListFromData:data
mutabilityOption:NSPropertyListImmutable
format:NSPropertyListXMLFormat_v1_0
errorDescription:nil];
if (array) {
NSMutableDictionary* myDict = [NSMutableDictionary dictionaryWithCapacity:[array count]];
for (NSDictionary* dict in array) {
MapDemoAnnotation* annotation = [[MapDemoAnnotation alloc]initWithDictionary:dict];
[mapView addAnnotation:annotation];
[annotation release];
}
NSLog(@"The count: %i", [myDict count]);
}
else {
NSLog(@"Plist does not exist");
}}
이 프로그램은 내가 알 수없는 이유로 충돌,하지만 난 재산 목록에 그렇지 않으면 MapDemoAnnotation 클래스에서 읽기에 뭔가 잘못이 있어야 그림. 나는 명백한 것을 놓치고 있는가, 초보자의 실수를하고 있는가? 내 코드는 대부분 빌려 왔기 때문에 내가 어떻게 접근하고 있는지 근거가 될 수 있습니다.
미리 감사드립니다.
그것은 일을하고, 두 번째 솔루션은 참으로 간단하고 안전한했다. 감사! – Jay