writeToFile正在覆盖以前的数据

Gra*_*ell 0 iphone objective-c nsdictionary nsfilemanager ios

我在我的代码中使用NSDictionary的writeToFile属性我正在尝试将字典更新到文档目录,如下所示,

NSDictionary *revisionDict = [NSDictionary dictionaryWithObject:currentItem.rev forKey:destPath];
NSArray *documentDirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentDirPath objectAtIndex:0];
NSString *dictPath = [documentsDir stringByAppendingPathComponent:@"Revision.dictionary"];
[revisionDict writeToFile:dictPath atomically:YES];
Run Code Online (Sandbox Code Playgroud)

这里的问题是我的字典,而不是每次迭代都被新条目更新,它会被新条目所覆盖.如何更新我的字典是否有任何替代writeToFile,任何帮助提前赞赏.

Ben*_*tto 8

记忆中的字典可以是可变的; 写入磁盘的字典是一整套信息的快照.

您不会从这样的任何框架方法中获得"合并更新"行为.如果您有要添加或更新的现有版本,则需要在内存中加载/将原始版本作为可变字典,然后对其进行更改或添加,然后保存整个新内容(-writeToFile:或其他).

如果要在循环中添加一堆条目,请先添加所有条目,然后在完成后将字典作为文件写入磁盘.