如何清除在XCode中创建的plist中的数据?

gur*_*ooj 5 iphone objective-c plist ios

我一直在使用plist在我的应用程序中存储数据.我已经能够从plist写入和读取没有问题.我在XCode中创建了这个plist,我自己添加了数字,字典和数组的行.但是,我希望能够将plist重置为原始状态,并且必须有一种更简单的方法来执行此操作,而不是为plist中的每个条目写入0或nil值.那么将plist重置为初始默认状态的最简单方法是什么?

Nic*_*ood 10

最简单的方法是使用NSFileManager删除文件,如下所示:

[[NSFileManager defaultManager] removeItemAtPath:plistPath error:NULL];
Run Code Online (Sandbox Code Playgroud)

或者如果您不想这样做,假设plist是一个字典,只需从应用程序包中加载一个,然后覆盖文档中的那个,如下所示:

NSDictionary *originalPlist = [NSDictionary dictionaryWithContentsOfFile:bundleFile];
[originalPlist writeToFile:documentsFile atomically:YES];
Run Code Online (Sandbox Code Playgroud)

这将用原始文件覆盖保存的文件.