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开发者_如何学Go 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];
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?
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?
try to change
NSFileManager *fileManager = [NSFileManager defaultManager];
to:
NSFileManager *fileManager = [NSFileManager new];
精彩评论