2012-02-17 32 views
0

我想檢查文檔目錄中是否存在.plist文件。如果它的確存在而不是,我想創建它並將其與第一個條目一起播種。如果它是否存在我想閱讀並附加條目。有條件地創建/讀取.plist文件

if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {  
    // read Faves file... 
    NSMutableDictionary *tempDict = [NSMutableDictionary dictionaryWithContentsOfFile:path]; 
    appDelegate.dictFaves = tempDict; 
} else { 
    NSLog(@"Creating Favorites file"); 
    BOOL result = [[NSFileManager defaultManager] createFileAtPath: path contents: (NSData *)appDelegate.dictFaves attributes: nil];    
} 

// .... and Append it to the list of existing favorites 
[appDelegate.dictFaves setObject:newFave forKey:key]; 
  1. createFileAtPath表示文件的回報FALSE沒有被創建。
  2. 我質疑鑄造appDelegate.dictFaves(NSDATA *)的有效性。如果這是不明智的,我如何創建一個Dictionary的文件?所以

    -[NSDictionary writeToURL:atomically:] 
    

    ,你可以說:

+0

鑄造'NSData的*'直接字典是無效的,該字典('dictFaves')包含什麼? – 2012-02-17 09:12:25

+0

@Vince - thx用於投射評論。 dictFaves包含與NSString對象配對的整數鍵。 – Bassman 2012-02-17 15:31:24

回答

0

您可以用它寫

[appDelegate.dictFaves writeToURL:url atomically:YES]; 

(假設appDelegate.dictFaves是一個有效的NSDictionary