본문 바로가기
모바일개발(Mobile Dev)/IOS개발(ObjectC)

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

by 테크한스 2015. 12. 12.
반응형



- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error

Description

Removes the file or directory at the specified path.


Prior to removing each item, the file manager asks its delegate if it should actually do so. It does this by calling the fileManager:shouldRemoveItemAtURL: method; if that method is not implemented (or the process is running in OS X 10.5 or earlier) it calls the fileManager:shouldRemoveItemAtPath: method instead. If the delegate method returns YES, or if the delegate does not implement the appropriate methods, the file manager proceeds to remove the file or directory. If there is an error removing an item, the file manager may also call the delegate’s fileManager:shouldProceedAfterError:removingItemAtURL: or fileManager:shouldProceedAfterError:removingItemAtPath: method to determine how to proceed.

Removing and item also removes all old versions of that item, invalidating any URLs returned by the URLForPublishingUbiquitousItemAtURL:expirationDate:error: method to old versions.

Parameters

path

A path string indicating the file or directory to remove. If the path specifies a directory, the contents of that directory are recursively removed. You may specify nil for this parameter.

error

On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.

Returns

YES if the item was removed successfully or if path was nil. Returns NO if an error occurred. If the delegate aborts the operation for a file, this method returns YES. However, if the delegate aborts the operation for a directory, this method returns NO.

Availability

iOS (2.0 and later)

Declared In

NSFileManager.h

Reference

NSFileManager Class Reference


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?

shareimprove this question

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.

shareimprove this answer
   
Thank you that was what I was looking for. I guess I just needed someone to explain it to me directly. – gurooj Jan 24 '12 at 7:02
   
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? – SRS Sep 21 '12 at 3:16
1 
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. – Nick Lockwood Sep 21 '12 at 14:45
1 
The new syntax for removing the file is now: [[NSFileManager defaultManager] removeItemAtPath:plistPath error:nil]; – fiacobelli Sep 25 '12 at 15:20 
 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];
shareimprove this answer

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

shareimprove this answer


반응형