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?
How do I clear the data in a plist created in Xcode?
Asked Answered
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.
Thank you that was what I was looking for. I guess I just needed someone to explain it to me directly. –
Molini
Is it possible to delete a single entry from a plist file?Basically I am storing dates in my plist and I want that, as the date stored the plist expires, that date/entry should be deleted from the plist and the plist should get refreshed.How can I do that? –
Gipps
You wouldn't delete it from the plist directly, you'd delete it from the dictionary by making a NSMutableDictionary using the mutableCopy method and then using the removeObjectForKey: method. Then just save the new dictionary over the top of the old plist. –
Emasculate
The new syntax for removing the file is now: [[NSFileManager defaultManager] removeItemAtPath:plistPath error:nil]; –
Wilks
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];
You could also try to just rename your Plist. Thats the least work i think.
© 2022 - 2024 — McMap. All rights reserved.