【问题标题】:MapKit based app crashing when loading from plist从 plist 加载时基于 MapKit 的应用程序崩溃
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 它奏效了,您的第二个解决方案确实更简单、更安全。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 2019-09-25
    • 2018-08-26
    • 1970-01-01
    • 2015-08-10
    相关资源
    最近更新 更多