Monday, April 22, 2013

NSFileManager removeItemAtURL example ios


removeItemAtURL :error:

Removes the file or directory at the specified URL.
- (BOOL)removeItemAtURL:(NSURL *)URL error:(NSError **)error
Parameters
URL
A file URL specifying the file or directory to remove. If the URL specifies a directory, the contents of that directory are recursively removed. You may specifynil 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.
Return Value of [NSFileManager removeItemAtURL]
YES if the item was removed successfully or if URL 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 returnsNO.
Discussion of [NSFileManager removeItemAtURL]
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’sfileManager:shouldProceedAfterError:removingItemAtURL: orfileManager:shouldProceedAfterError:removingItemAtPath: method to determine how to proceed.
Example of [NSFileManager removeItemAtURL]

- (void)resetCoreData;
{
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"deleteme.sqlite"];

NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtURL:storeURL error:NULL];
    NSError* error = nil;

    if([fileManager fileExistsAtPath:[NSString stringWithContentsOfURL:storeURL encoding:NSASCIIStringEncoding error:&error]])
    {
        [fileManager removeItemAtURL:storeURL error:nil];
    }

self.managedObjectContext = nil;
self.persistentStoreCoordinator = nil;
}