开发者

MapKit based app crashing when loading from plist

开发者 https://www.devze.com 2023-02-23 07:14 出处:网络
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

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.

Here is my annotation implementation:

#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 

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");
    }}

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? M开发者_如何学编程y code is largely borrowed, so I could be way off base with how I'm approaching it.

Thanks in advance!


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

However, the documentation mentions that the above method is obsolete and you should use propertyListWithData:options:format:error: instead.


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

精彩评论

暂无评论...
验证码 换一张
取 消