[英]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];
createFileAtPath
returns FALSE
meaning the file was not created. createFileAtPath
返回FALSE
表示未创建文件。 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.