2011-04-08 8 views
1

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 클래스에서 읽기에 뭔가 잘못이 있어야 그림. 나는 명백한 것을 놓치고 있는가, 초보자의 실수를하고 있는가? 내 코드는 대부분 빌려 왔기 때문에 내가 어떻게 접근하고 있는지 근거가 될 수 있습니다.

미리 감사드립니다.

답변

1

propertyListFromData 호출의 세 번째 매개 변수가 잘못되었습니다. 컴파일러는 format 매개 변수가 NSPropertyListFormat 변수에 대한 포인터를 필요로하기 때문에 "캐스트없이 정수에서 포인터를 만듭니다"라는 경고를 주어야합니다. 따라서 메소드는 을 반환 할 수 있습니다. 형식을 사용할 수 있습니다. 그래서 당신은 할 필요가 :

NSPropertyListFormat propertyListFormat; 
NSMutableArray* array = [NSPropertyListSerialization 
    propertyListFromData:data 
    mutabilityOption:NSPropertyListImmutable 
    format:&propertyListFormat 
    errorDescription:nil]; 

그러나, 문서는 위의 방법이 사용되지 않습니다 것을 언급하고 대신 propertyListWithData:options:format:error:를 사용해야합니다.


그러나, 그것은 단지 대신있는 NSArray의 initWithContentsOfFile: 메서드를 호출하는 것이 훨씬 더 쉽다 :

NSString *plistPath = [[NSBundle mainBundle] pathForResource... 

NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath]; 

if (array) { 
    //your existing code here... 
} 
else { 
    NSLog(@"Plist does not exist"); 
} 

[array release]; 
+0

그것은 일을하고, 두 번째 솔루션은 참으로 간단하고 안전한했다. 감사! – Jay

관련 문제