【发布时间】:2011-04-08 20:25:40
【问题描述】:
我正在编写一个程序,它使用 MapKit 来显示地图,该地图将从 plist 文件中加载自定义注释。每个注释都是根数组中的一个字典项,具有标题、副标题、纬度和经度。当我出于测试目的在注释中硬编码时,程序运行良好。但是随着 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 类时一定做错了。我是否遗漏了一些明显的东西,或者犯了一个新手错误? 我的代码很大程度上是借来的,所以我可能会偏离我的处理方式。
提前致谢!
【问题讨论】:
标签: arrays xcode ios mapkit plist