簡體   English   中英

unarchiveObjectWithFile保留/自動釋放需要嗎?

[英]unarchiveObjectWithFile retain / autorelease needed?

如果可以的話,只是一個快速的內存管理問題...下面的代碼是否正確,或者我應該進行保留和自動釋放,我有應有的感覺。 但是按照規則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;
    }

您是正確的,因為unarchiveObjectWithFile返回一個自動釋放的對象,因為它不包含newcopyalloc

這是一個稍微重寫的版本,以使用常見的Objective-C格式習慣用法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM