Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone/Objective C: Can't delete a file

In my application, I let the user record a sound clip and later, if the user chooses, I want him to be able to delete it.

This is the code I use:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSLog(@"File exists: %d", [fileManager fileExistsAtPath:path]);
NSLog(@"Is deletable file at path: %d", [fileManager isDeletableFileAtPath:path]);
[fileManager removeItemAtPath:path error:&error];
if (error != nil)
{
    NSLog(@"Error: %@", error);
    NSLog(@"Path to file: %@", path);
}

The problem is that fileExistsAtPath and isDeletableFileAtPath return null and the removeItemAtPath doesn't work, and throws this error,

Error: Error Domain=NSCocoaErrorDomain Code=4 UserInfo=0x391b7f0 "Operation could not be completed. (Cocoa error 4.)"

The path has this form:

/Users/andrei/Library/Application%20Support/iPhone%20Simulator/User/Applications/5472B318-FA57-4F8D-AD91-7E06E9609215/Documents/1280913694.caf

There is a file there called 1280913694.caf, but it doesn't pick it up. Does it have something to do with the way in which the path should be represented?

The path works when playing the audio file with AVAudioPlayer.

I've also changed the %@ to %d for fileExistsAtPath and isDeletableFileAtPath and the answer is 0, which I suppose means FALSE.

The name of the file is stored in a database, and the path to the file is retrieved with this method:

-(NSString *)returnFullPathToDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return documentsDirectory;
}

After I get this value, I use it in the following code

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
like image 872
Alex Avatar asked Aug 04 '10 10:08

Alex


1 Answers

Your check for (error != nil) is incorrect. You should set a BOOL to the return value of the method and use that to handle error conditions as it is possible for the method to complete successfully and for error to be non nil afterwards. So, the file might actually have been deleted but you are getting an incorrect error back.

You should also not try to delete the file if it doesn't exist.

Also, I usually just log the error's localizedDescription as that is easier to read

This code works in my project (path was defined elsewhere):

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    BOOL fileExists = [fileManager fileExistsAtPath:path];
    NSLog(@"Path to file: %@", path);        
    NSLog(@"File exists: %d", fileExists);
    NSLog(@"Is deletable file at path: %d", [fileManager isDeletableFileAtPath:path]);
    if (fileExists) 
    {
        BOOL success = [fileManager removeItemAtPath:path error:&error];
        if (!success) NSLog(@"Error: %@", [error localizedDescription]);
    }

related answer: NSError: Does using nil to detect Error actually turn off error reporting?

like image 119
Jesse Clark Avatar answered Nov 08 '22 10:11

Jesse Clark