【发布时间】:2010-12-10 10:35:02
【问题描述】:
我正在开发一个在地图上绘制叠加层的 iPhone 应用程序。 我想将包含覆盖的数组保存在 NSMutableDictionary 中,但只有我保存的最后一个条目有效:
// 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 parsed from the KML file to the map.
[map addOverlays:[overlaysDictionary objectForKey:key]];
}
显然是内存问题,可惜我不知道如何保留字典中存储的数据。
这是 KML 解析器中代码的摘录,取自 Apple 示例:
- (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];
}
提前谢谢你!
【问题讨论】:
-
kml变量在哪里以及如何定义? -
头文件中定义了kml变量:KMLParser *kml;如果可能的话,我不想保存整个解析器,而只想保存输出(NSArray 覆盖)
-
在运行此代码时至少发生一次泄漏。这可能是问题吗?
标签: objective-c cocoa memory-leaks nsmutabledictionary