Ios 有条件地创建/读取.plist文件
我想检查Documents目录中是否存在.plist文件。如果它不存在,我想创建它并用第一个条目为它种子。如果它确实存在,我想阅读它并在其中添加一个条目Ios 有条件地创建/读取.plist文件,ios,createfile,Ios,Createfile,我想检查Documents目录中是否存在.plist文件。如果它不存在,我想创建它并用第一个条目为它种子。如果它确实存在,我想阅读它并在其中添加一个条目 if ([[NSFileManager defaultManager] fileExistsAtPath:path]) { // read Faves file... NSMutableDictionary *tempDict = [NSMutableDictionary dictionaryWithContent
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
返回FALSE
表示文件未创建(NSDATA*)
的有效性。如果这是不明智的,如何使用字典创建文件
您可以使用以下方法编写:
-[NSDictionary writeToURL:atomically:]
所以,你可以说:
[appDelegate.dictFaves writeToURL:url atomically:YES];
(假设
appDelegate.dictFaves
是有效的NSDictionary
)直接转换到NSData*
字典无效,该字典(dictFaves
)包含哪些内容?@Vince-thx用于转换注释。dictFaves包含与NSString对象配对的整数键。