NSMutableDictionary Memory Problem
I'm working on an iPhone app drawing overlays on a map. I want to save the arrays which contain the overlays in an NSMutableDictionary, but only the last entry I saved works:
// Get "Kurwege" from CoreData
NSError * error;
if(![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]); // TODO
}
if(self.overlaysDictionary == nil) {
self.overlaysDictionary = [[NSMutableDictionary alloc] initWithCapacity:[fetchedResultsController.fetchedObjects count]];
}
NSString *path;
for (Kurweg *kurweg in fetchedResultsController.fetchedObjects) {
// Locate the path to the .kml file in the application's bundle
// and parse it with the KMLParser.
path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", [[kurweg valueForKey:@"kmlfile"] description]] ofType:@"kml"];
kml = [[KMLParser parseKMLAtPath:path] retain]; // PROBLEM: Executing this the second time seems to override my data stored in the dictionary
// Save overlays in dictionary
[self.overlaysDictionary setObject:[kml overlays] forKey:[NSString stringWithFormat:@"%@", [[kurweg valueForKey:@"kmlfile"] description]]];
}
// Draw overlays
NSArray *keys = [overlaysDictionary allKeys];
for (NSString *key in keys) {
// Add all of the MKOverlay objects par开发者_如何学编程sed from the KML file to the map.
[map addOverlays:[overlaysDictionary objectForKey:key]];
}
Obviously it's a memory problem, but unfortunately I don't know how to retain the data stored in the dictionary.
This is an excerpt from the code in the KML parser, taken from an Apple example:
- (NSArray *)overlays {
NSMutableArray *overlays = [[NSMutableArray alloc] init];
for (KMLPlacemark *placemark in _placemarks) {
id <MKOverlay> overlay = [placemark overlay];
if (overlay)
[overlays addObject:overlay];
}
return [overlays autorelease];
}
Thank you in advance!
My guess would be that [[kurweg valueForKey:@"kmlfile"] description]]
is always returning the same value and so you only ever have one value in your dictionary.
精彩评论