Objective c NSMutableDictionary问题
假设我有一个有两个NSMUTABLE字典的类Objective c NSMutableDictionary问题,objective-c,cocoa,release,Objective C,Cocoa,Release,假设我有一个有两个NSMUTABLE字典的类 @interface GameDataManager : CCNode { NSMutableDictionary *worldInfoDictionary; NSMutableDictionary *levelInfoDictionary; } @property (nonatomic, retain) NSMutableDictionary *worldInfoDictionary; @property (nonatomic,
@interface GameDataManager : CCNode {
NSMutableDictionary *worldInfoDictionary;
NSMutableDictionary *levelInfoDictionary;
}
@property (nonatomic, retain) NSMutableDictionary *worldInfoDictionary;
@property (nonatomic, retain) NSMutableDictionary *levelInfoDictionary;
当应用程序启动时,我从plist加载数据,如下所示:
-(BOOL) readApplicationData:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease];
if (myData == nil) {
return NO;
}
NSKeyedUnarchiver *un = [[NSKeyedUnarchiver alloc] initForReadingWithData:myData];
NSMutableDictionary *dic = [un decodeObjectForKey:@"GameData"];
self.worldInfoDictionary = [dic objectForKey:@"WorldInfoDictionary"];
self.levelInfoDictionary = [dic objectForKey:@"LevelInfoDictionary"];
for (int i = 1; i <= 10; i++) {
WorldInfoData * w = [worldInfoDictionary objectForKey:
[[[NSString alloc] initWithFormat:@"World%d", i] autorelease]];
NSLog(@"%d", w.worldNum);
[w release];
}
[un finishDecoding];
[un release];
return YES;
}
NSMutableDictionary *dic = [GameDataManager sharedGameDataManager].worldInfoDictionary;
for (int i = 1; i <= 10; i++) {
NSString *key = [[NSString alloc] initWithFormat:@"World%d", i];
WorldInfoData *worldInfoData = [dic objectForKey:key];
if (worldInfoData.worldNum == 1) {
//......
}
[key release];
}
我可以看出两个字典中的值/键的数量是正确的
然后我尝试打印出每个对象的所有“worldNum”值,但应用程序除外
crahed:
如果我取消对此行的注释:
[un release];
应用程序工作正常,没有崩溃
谁能给我解释一下,谢谢^_^
谢谢你的回答
但如果我删除了用于输出某些信息的代码行:
for (int i = 1; i <= 10; i++) {
WorldInfoData * w = [worldInfoDictionary objectForKey:
[[[NSString alloc] initWithFormat:@"World%d", i] autorelease]];
NSLog(@"%d", w.worldNum);
[w release];
}
对于(inti=1;i首先,我建议在启用僵尸检测的情况下运行应用程序
您将发现一个通知,说明您正在过度删除“w”
WorldInfoData * w = [worldInfoDictionary objectForKey:
[[[NSString alloc] initWithFormat:@"World%d", i] autorelease]];
NSLog(@"%d", w.worldNum);
[w release];
将“w”添加到自动释放池中,然后显式地调用release;line将修复您的问题或不向自动释放池中添加“w”。finishDecoding函数中有什么?这是NSKeyEdunachiver的函数…几乎…被显式自动释放的东西是键。但是w
在它不是此代码所拥有的对象时被释放。
NSMutableDictionary *dic = [GameDataManager sharedGameDataManager].worldInfoDictionary;
for (int i = 1; i <= 10; i++) {
NSString *key = [[NSString alloc] initWithFormat:@"World%d", i];
WorldInfoData *worldInfoData = [dic objectForKey:key];
if (worldInfoData.worldNum == 1) {
//......
}
[key release];
}
WorldInfoData * w = [worldInfoDictionary objectForKey:
[[[NSString alloc] initWithFormat:@"World%d", i] autorelease]];
NSLog(@"%d", w.worldNum);
[w release];