gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:49:36 25 4
gpt4 key购买 nike

我正在编写一个使用 MapKit 显示 map 的程序,该 map 将从 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 类时做错了什么。我是否遗漏了一些明显的东西,或者犯了一个新手错误?我的代码大部分是借来的,所以我可能会偏离我处理它的方式。

提前致谢!

最佳答案

propertyListFromData 调用中的第三个参数错误。编译器必须在那里给你一个“从整数生成指针而不进行强制转换”的警告,因为格式参数需要一个指向 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];

关于arrays - 从 plist 加载时基于 MapKit 的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5600419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com