UnarchiveObjectWithFile save / abstract required?
Just a quick memory management question if I can ... Is the code below ok, or should I do save and auto-update, I get the feel I should. But according to the rules unarchiveObjectWithFile
it is not contained new
, copy
or alloc
.
-(NSMutableArray *)loadGame {
if([[NSFileManager defaultManager] fileExistsAtPath:[self pathForFile:@"gameData.plist"]]) {
NSMutableArray *loadedGame = [NSKeyedUnarchiver unarchiveObjectWithFile:[self pathForFile:@"gameData.plist"]];
return loadedGame;
} else return nil;
}
or
-(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;
}
a source to share
You are correct in unarchiveObjectWithFile
returning an auto-implemented object since it does not contain new
, copy
or alloc
.
Here's a version that's slightly rewritten to use common Objective-C formatting idioms:
- (NSMutableArray *)loadGame {
NSString *gameDataPath = [self pathForFile:@"gameData.plist"];
if([[NSFileManager defaultManager] fileExistsAtPath:gameDataPath]) {
return [NSKeyedUnarchiver unarchiveObjectWithFile:gameDataPath];
}
return nil;
}
a source to share