Devang Devang - 3 months ago 17
iOS Question

NSFileManager not deleting a file that exists

I am having some problem with using the NSFileManager functionalities.
This happens both on the Simulator and an iPhone device (iOS 5.1).

Basically, I have a bunch of files that are stored in a document that I created. Now I am trying to move a file (stored at path) to the same directory with another name, to check if the deletion works.

if ([[NSFileManager defaultManager] isDeletableFileAtPath:path]) {
BOOL success = [[NSFileManager defaultManager] moveItemAtPath:path toPath:[path stringByAppendingString:@".deleted"] error:&error];
if (!success) {
NSLog(@"Error removing file at path: %@", error.localizedDescription);
}
}


The output of this is both files at path and path.deleted.
I ultimately just want to remove the file using removeItemAtPath but that is not working. It returns a success but if I see it in the file directory I can still see it there even after an hour.

cnu cnu
Answer

If you want to delete a file you should use removeItemAtPath:myPath error:NULL like

NSError *error;
if ([[NSFileManager defaultManager] isDeletableFileAtPath:path]) {
    BOOL success = [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
    if (!success) {
        NSLog(@"Error removing file at path: %@", error.localizedDescription);
    }
}