开发者

Saving MKOverlayView array -> plist

I have an application that involves lots of MKOverlayViews. Every time one is added to the map, it is also added to an NSArray. When the user wishes to stop adding overlays, I would like for them to be able to save them, and be able to access them later. How can I store an array of MKOverlayViews into a plist, and then reload them back into a mapView later? Is this possibl开发者_JAVA百科e?

I'm attempting to use this code to take the MKPolylineViews from the array and add the corresponding MKPolylines to the map, but it crashes at the '[mapView addOverlay....' line.

Writing array to plist:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if(overlays)
{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:overlays];
    [defaults setObject:data forKey:@"theKey"];
    [defaults synchronize];
}

Reading data from plist:

NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"theKey"];
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithData:data];

for(MKPolylineView* a in arr)
    [mapView addOverlay:a.polyline];


I'd look at archiving with NSArchiver or NSKeyedArchiver and then writing them out to disk. Probably better than trying to squeeze them into a plist somehow...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜