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

时间:2022-10-27 19:17:56

I have been using a plist to store data in my app. I have been able to write and read from the plist with no problem. I created this plist in XCode, adding the rows of numbers, dictionaries, and arrays myself. However, I would like to be able to reset the plist to the original state, and there must be an easier way to do this than writing a 0 or nil value to every entry in the plist. So what is the easiest way to reset the plist to its initial default state?

我一直在使用plist在我的app中存储数据,我可以毫无问题地从plist中读写数据。我在XCode中创建了这个plist,添加了数字、字典和数组。但是,我希望能够将plist重置为原始状态,并且必须有一种更简单的方法来实现这一点,而不是将一个0或nil值写入plist中的每个条目。那么,将plist重置为初始默认状态的最简单方法是什么?

3 个解决方案

#1


10  

The simplest thing would be to delete the file using NSFileManager, like this:

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

[[NSFileManager defaultManager] removeItemAtPath:plistPath error:NULL];

Or if you don't want to do that, assuming the plist is a dictionary, just load the one from your application bundle and then overwrite the one in your documents, like this:

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

NSDictionary *originalPlist = [NSDictionary dictionaryWithContentsOfFile:bundleFile];
[originalPlist writeToFile:documentsFile atomically:YES];

Which will overwrite the saved file with the original file.

它将用原始文件覆盖已保存的文件。

#2


1  

 NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:@"mobile-watchlist.plist"];
    [fileManager removeItemAtPath: fullPath error:NULL];

#3


0  

You could also try to just rename your Plist. Thats the least work i think.

您也可以尝试重命名您的Plist。那是我认为最少的工作。

#1


10  

The simplest thing would be to delete the file using NSFileManager, like this:

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

[[NSFileManager defaultManager] removeItemAtPath:plistPath error:NULL];

Or if you don't want to do that, assuming the plist is a dictionary, just load the one from your application bundle and then overwrite the one in your documents, like this:

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

NSDictionary *originalPlist = [NSDictionary dictionaryWithContentsOfFile:bundleFile];
[originalPlist writeToFile:documentsFile atomically:YES];

Which will overwrite the saved file with the original file.

它将用原始文件覆盖已保存的文件。

#2


1  

 NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:@"mobile-watchlist.plist"];
    [fileManager removeItemAtPath: fullPath error:NULL];

#3


0  

You could also try to just rename your Plist. Thats the least work i think.

您也可以尝试重命名您的Plist。那是我认为最少的工作。