unarchiveObjectWithFile是否需要保留/自动释放?

时间:2010-04-19 10:49:15

标签: iphone objective-c cocoa-touch

如果我可能只是一个快速的内存管理问题......下面的代码是否正常,或者我应该保留和自动发布,我得到了我应该的感觉。但根据规则unarchiveObjectWithFile不包含newcopyalloc

-(NSMutableArray *)loadGame {
    if([[NSFileManager defaultManager] fileExistsAtPath:[self pathForFile:@"gameData.plist"]]) {
        NSMutableArray *loadedGame = [NSKeyedUnarchiver unarchiveObjectWithFile:[self pathForFile:@"gameData.plist"]];
        return loadedGame;
    } else return nil;
}

-(NSMutableArray *)loadGame {
        if([[NSFileManager defaultManager] fileExistsAtPath:[self pathForFile:@"gameData.plist"]]) {
            NSMutableArray *loadedGame = [[NSKeyedUnarchiver unarchiveObjectWithFile:[self pathForFile:@"gameData.plist"]] retain];
            return [loadedGame autorelease];
        } else return nil;
    }

1 个答案:

答案 0 :(得分:4)

你是正确的unarchiveObjectWithFile返回一个自动释放的对象,因为它不包含newcopyalloc

这是一个稍微重写的版本,使用常见的Objective-C格式化习语:

- (NSMutableArray *)loadGame {
    NSString *gameDataPath = [self pathForFile:@"gameData.plist"];
    if([[NSFileManager defaultManager] fileExistsAtPath:gameDataPath]) {
        return [NSKeyedUnarchiver unarchiveObjectWithFile:gameDataPath];
    }
    return nil;
}