基于MapKit从应用程序加载的plist崩溃时 [英] MapKit based app crashing when loading from plist

查看:248
本文介绍了基于MapKit从应用程序加载的plist崩溃时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写它使用MapKit以显示地图,这将加载自定义注释从plist文件的程序。每个注释是根阵列中的字典项,与一个标题,副标题,纬度和经度。当我硬在注释用于测试目的codeD时,程序精美的工作。但增加的MapDemoAnnotation类和我尝试在属性列表中读取数据,程序崩溃时推出。

I'm writing a program which uses MapKit to display a map which will load custom annotations from a plist file. Each annotation is a dictionary item in the root array, with a title, subtitle, latitude, and longitude. When I hard-coded in the annotations for test purposes, the program worked beautifully. But with the addition of the MapDemoAnnotation class and my attempt to read in the property list, the program crashes upon launch.

下面是我的注释实现:

#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 

我猜viewDidLoad方法在我的RootViewController的类的问题,虽然。

I'm guessing the viewDidLoad method in my RootViewController class is the problem, though.

- (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类读取。我失去了一些东西很明显,或者使一个新手的错误?
我的code在很大程度上是借来的,所以我可以用我如何接近它是大错特错。

The program crashes for reasons I cannot figure, but I figure I must have done something wrong in reading in the property list or else in the MapDemoAnnotation class. Am I missing something obvious, or making a novice mistake? My code is largely borrowed, so I could be way off base with how I'm approaching it.

在此先感谢!

推荐答案

在调用 propertyListFromData 第三个参数是错误的。编译器必须给你一个使指针从整数,未作类型转换警告有因为格式参数期望一个指向一个NSPropertyListFormat变量(所以该方法可以的收益的格式给你)。所以,你需要做的:

The third parameter in the call to propertyListFromData is wrong. The compiler must be giving you a "makes pointer from integer without a cast" warning there because the format parameter expects a pointer to a NSPropertyListFormat variable (so the method can return the format to you). So you need to do:

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

不过,文件中提到,上述方法已过时,你应该使用 propertyListWithData:选择:格式:错误:而不是

结果
然而,它更容易只是调用NSArray的 initWithContentsOfFile:方法,而不是:


However, it's much easier to just call NSArray's initWithContentsOfFile: method instead:

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

这篇关于基于MapKit从应用程序加载的plist崩溃时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆