gurooj
gurooj

Reputation: 2110

How do I clear the data in a plist created in Xcode?

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?

Upvotes: 5

Views: 6962

Answers (3)

Nick Lockwood
Nick Lockwood

Reputation: 40995

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

[[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:

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

Which will overwrite the saved file with the original file.

Upvotes: 10

user3759274
user3759274

Reputation: 1

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

Upvotes: 0

Vijay Maurya
Vijay Maurya

Reputation: 19

 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];

Upvotes: 1

Related Questions