2011-04-08 54 views
1

我正在寫使用MapKit顯示地圖,這將加載自定義註釋從plist文件中的程序加載時崩潰。每個註釋都是根數組中的字典項目,帶有標題,副標題,緯度和經度。當我爲了測試目的對註釋進行硬編碼時,該程序運行得非常好。但是,隨着MapDemoAnnotation類的添加以及我嘗試讀取屬性列表,該程序在啓動時崩潰。MapKit應用從plist中

這裏是我的註解實現:

#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類閱讀。我是否錯過了明顯的東西,或者犯了新手錯誤? 我的代碼很大程度上是借用的,所以我可以基於我如何接近它。

在此先感謝!

回答

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

它的工作,和你的第二個解決方案確實是簡單和更安全。謝謝! – Jay 2011-04-14 14:23:17