2010-04-19 86 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

相關問題