2012-02-17 63 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