从plist加载时基于MapKit的应用程序崩溃

我正在编写一个程序,它使用MapKit来显示一个地图,它将从plist文件中加载自定义的注释。 每个注释都是根数组中的字典项目,带有标题,副标题,纬度和经度。 当我为了testing目的对注释进行硬编码时,程序运行的很好。 但是,随着MapDemoAnnotation类的添加以及我尝试读取属性列表,程序在启动时崩溃。

这是我的注释实现:

#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类。 我是否错过了一些明显的东西,或者犯了一个新手的错误? 我的代码很大程度上是借用的,所以我可以基于我如何接近它。

提前致谢!

在调用propertyListFromData的第三个参数是错误的。 编译器必须给你一个“不带转换的整型指针”警告,因为format参数需要一个指向NSPropertyListFormatvariables的指针(所以这个方法可以返回格式给你)。 所以你需要做的是:

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