简体   繁体   English

有条件地创建/读取.plist文件

[英]conditionally create/read a .plist file

I want to check for the presence of a .plist file in the Documents directory. 我想检查Documents目录中是否存在.plist文件。 If it does not exist, I want to create it and seed it with the 1st entry. 如果它存在,我要创造,并与1号种子进入它。 If it does exist I want to read it and append an entry to it. 如果确实存在,我想阅读并在其中添加一个条目。

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 returns FALSE meaning the file was not created. createFileAtPath返回FALSE表示未创建文件。
  2. I question the validity of casting appDelegate.dictFaves to (NSDATA *) . 我怀疑铸造的有效性appDelegate.dictFaves(NSDATA *) If that is unwise, how to I create a file with a Dictionary ? 如果这是不明智的,如何创建带有Dictionary的文件?

You can write it using: 您可以使用以下代码编写:

-[NSDictionary writeToURL:atomically:]

So, you can say: 因此,您可以说:

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

(assuming appDelegate.dictFaves is a valid NSDictionary ) (假设appDelegate.dictFaves是有效的NSDictionary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM