Iphone 是否需要文件保留/自动删除的UnarchiveObjects?
只是一个快速的内存管理问题,如果我可以。。。下面的代码正常吗,或者我应该做一个保留和自动释放,我觉得我应该。但根据规则,未存档的objectwithfile不包含Iphone 是否需要文件保留/自动删除的UnarchiveObjects?,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,只是一个快速的内存管理问题,如果我可以。。。下面的代码正常吗,或者我应该做一个保留和自动释放,我觉得我应该。但根据规则,未存档的objectwithfile不包含新建、复制或所有 -(NSMutableArray *)loadGame { if([[NSFileManager defaultManager] fileExistsAtPath:[self pathForFile:@"gameData.plist"]]) { NSMutableArray *loadedGam
新建
、复制
或所有
-(NSMutableArray *)loadGame {
if([[NSFileManager defaultManager] fileExistsAtPath:[self pathForFile:@"gameData.plist"]]) {
NSMutableArray *loadedGame = [NSKeyedUnarchiver unarchiveObjectWithFile:[self pathForFile:@"gameData.plist"]];
return loadedGame;
} else return nil;
}
或
正确的做法是,
unarchiveObjectWithFile
返回一个自动删除的对象,因为它不包含new
、copy
或alloc
-(NSMutableArray *)loadGame {
if([[NSFileManager defaultManager] fileExistsAtPath:[self pathForFile:@"gameData.plist"]]) {
NSMutableArray *loadedGame = [NSKeyedUnarchiver unarchiveObjectWithFile:[self pathForFile:@"gameData.plist"]];
return loadedGame;
} else return nil;
}
以下是一个版本,它稍微经过重新编写,使用了常见的Objective-C格式习惯用法:
- (NSMutableArray *)loadGame {
NSString *gameDataPath = [self pathForFile:@"gameData.plist"];
if([[NSFileManager defaultManager] fileExistsAtPath:gameDataPath]) {
return [NSKeyedUnarchiver unarchiveObjectWithFile:gameDataPath];
}
return nil;
}
非常感谢Nick,也感谢您提供正确格式的提示。非常感谢。