9

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];
tshepang
  • 12,111
  • 21
  • 91
  • 136
Alex
  • 7,432
  • 20
  • 75
  • 118
  • 1
    Please change the two `%@` before `-removeItemAtPath` to `%d` and run again. – kennytm Aug 04 '10 at 10:42
  • note that the file:// prefix must be removed, i.e. convert to NSURL and get the .path attribute (as indeed the path is above) – ThomasRS Aug 18 '11 at 15:06

3 Answers3

27

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?

Community
  • 1
  • 1
Jesse Clark
  • 1,150
  • 2
  • 13
  • 15
0

The url encoded file path looks suspicious to me. I read somewhere that iPhone file paths should be represented as URL these days, but I don't think spaces need to be url encoded.

Try removing the %20's from your path and trying again?

Jasarien
  • 58,279
  • 31
  • 157
  • 188
  • 1
    I let the file path be: **/Users/andrei/Library/Application Support/iPhone Simulator/User/Applications/0547AE74-AD10-4BEF-98CF-8FFF5BB2F70F/Documents/1280913694.caf** so, no %20, and now...which is weird, it tells me that the file does not exist (returns 0 at fileExistsAtPath), but also that the file is deletable (returns 1 at isDeletableFileAtPath). The error still remains and is not deleting the file. – Alex Aug 04 '10 at 11:07
  • Could you please post the code you're using to retrieve the path? – Jasarien Aug 04 '10 at 11:13
  • Are you able to test this on the device to rule out some strange issue involving the simulator's application path? – Jasarien Aug 04 '10 at 11:36
  • Nope, I can't, I don't have a testing device, just XCode. – Alex Aug 04 '10 at 11:43
  • I can't see anything obviously wrong in the code, so I don't know what to suggest other than trying to test it on a device, if you can get hold of one. A second hand iPod touch would be sufficient, and quite cheap. – Jasarien Aug 04 '10 at 13:43
  • In a terminal, try ls -l /Users/andrei/Library/Application Support/iPhone Simulator/User/Applications/0547AE74-AD10-4BEF-98CF-8FFF5BB2F70F/Documents/1280913694.caf and see what the permissions say. They should look something like this: "-rw-r--r--" followed by a number and then your username and "staff" as separate words. – Kalle Aug 04 '10 at 15:15
-8

try to change

NSFileManager *fileManager = [NSFileManager defaultManager];

to:

NSFileManager *fileManager = [NSFileManager new];
iyusa
  • 1