2010-04-19 121 views
2

只是一个快速的内存管理问题,如果我可能...是否代码低于确定,或者我应该做一个保留和autorelease,我觉得我应该。但根据规则unarchiveObjectWithFile不包含new,copyallocunarchiveObjectWithFile保留/ autorelease需要?

-(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; 
    } 

回答

4

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

下面是稍微重新编写,使用普通的Objective-C格式成语版本:

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

非常感谢尼克,也感谢您对格式正确的指针。非常感激。 – fuzzygoat 2010-04-19 11:05:06