iPhone / Objective C:无法删除文件

时间:2021-08-24 20:18:39

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,

问题是fileExistsAtPath和isDeletableFileAtPath返回null并且removeItemAtPath不起作用,并抛出此错误,

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

错误:错误域= NSCocoaErrorDomain代码= 4 UserInfo = 0x391b7f0“操作无法完成。(可可错误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?

那里有一个名为1280913694.caf的文件,但它没有提取它。它是否与路径的表示方式有关?

The path works when playing the audio file with AVAudioPlayer.

使用AVAudioPlayer播放音频文件时,该路径有效。

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

我还将%@更改为%d为fileExistsAtPath和isDeletableFileAtPath,答案为0,我认为这意味着为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];

3 个解决方案

#1


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.

您的检查(错误!=无)是不正确的。您应该将BOOL设置为方法的返回值,并使用它来处理错误条件,因为方法可以成功完成,之后错误可以是非零。因此,该文件可能实际上已被删除,但您收到的错误信息不正确。

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

另外,我通常只记录错误的localizedDescription,因为它更容易阅读

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?

相关回答:NSError:使用nil检测错误实际上是否关闭了错误报告?

#2


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.

url编码的文件路径对我来说很可疑。我在某处读到iPhone文件路径应该表示为URL这些天,但我不认为空格需要进行url编码。

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

尝试从路径中删除%20,然后再试一次?

#3


-8  

try to change

试着改变

NSFileManager *fileManager = [NSFileManager defaultManager];

to:

NSFileManager *fileManager = [NSFileManager new];

#1


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.

您的检查(错误!=无)是不正确的。您应该将BOOL设置为方法的返回值,并使用它来处理错误条件,因为方法可以成功完成,之后错误可以是非零。因此,该文件可能实际上已被删除,但您收到的错误信息不正确。

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

另外,我通常只记录错误的localizedDescription,因为它更容易阅读

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?

相关回答:NSError:使用nil检测错误实际上是否关闭了错误报告?

#2


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.

url编码的文件路径对我来说很可疑。我在某处读到iPhone文件路径应该表示为URL这些天,但我不认为空格需要进行url编码。

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

尝试从路径中删除%20,然后再试一次?

#3


-8  

try to change

试着改变

NSFileManager *fileManager = [NSFileManager defaultManager];

to:

NSFileManager *fileManager = [NSFileManager new];