Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c iPhone/Objective C:Can';不要删除文件_Objective C_Iphone_File_File Io - Fatal编程技术网

Objective c iPhone/Objective C:Can';不要删除文件

Objective c iPhone/Objective C:Can';不要删除文件,objective-c,iphone,file,file-io,Objective C,Iphone,File,File Io,在我的应用程序中,我让用户录制一个声音片段,然后,如果用户选择,我希望他能够删除它 这是我使用的代码: NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error; NSLog(@"File exists: %d", [fileManager fileExistsAtPath:path]); NSLog(@"Is deletable file at path: %d", [fileManager isDe

在我的应用程序中,我让用户录制一个声音片段,然后,如果用户选择,我希望他能够删除它

这是我使用的代码:

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);
}
问题是
fileExistsAtPath
isDeletableFileAtPath
返回null,并且
removietematpath
不工作,并抛出此错误

错误:错误域=NSCOCAERRORDOMAIN Code=4 UserInfo=0x391b7f0“操作无法完成。(可可错误4)”

路径具有以下形式:

/Users/andrei/Library/Application%20Support/iPhone%20Simulator/User/Applications/5472B318-FA57-4F8D-AD91-7E06E9609215/Documents/1280913694.caf
那里有一个名为
1280913694.caf
的文件,但它没有拾取它。它是否与路径的表示方式有关

使用
AVAudioPlayer
播放音频文件时,该路径起作用

我还将
fileExistsAtPath
isDeletableFileAtPath
%@
更改为
%d
,答案是0,我想这意味着FALSE

文件名存储在数据库中,使用以下方法检索文件路径:

-(NSString *)returnFullPathToDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return documentsDirectory;
}
得到这个值后,我在下面的代码中使用它

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

url编码的文件路径看起来可疑。我在某个地方读到,现在iPhone文件路径应该表示为URL,但我认为空间不需要URL编码

尝试从路径中删除%20并重试?

尝试更改

NSFileManager *fileManager = [NSFileManager defaultManager];
致:

您对(错误!=nil)的检查不正确。您应该将BOOL设置为方法的返回值,并使用该值来处理错误条件,因为方法可能会成功完成,并且错误可能会在之后变为非零。因此,该文件实际上可能已被删除,但返回的错误不正确

如果文件不存在,也不应尝试删除该文件

此外,我通常只记录错误的localizedDescription,因为这样更容易阅读

此代码适用于我的项目(路径在别处定义):

相关答复:

请将
-removietematpath
之前的两个
%@
更改为
%d
,然后再次运行。请注意,必须删除文件://前缀,即转换为NSURL并获取.path属性(因为路径确实在上面)我让文件路径为:/Users/andrei/Library/Application Support/iPhone Simulator/User/Applications/0547AE74-AD10-4BEF-98CF-8FFF5BB2F70F/Documents/1280913694.caf所以,没有%20,现在…奇怪的是,它告诉我文件不存在(在fileExistsAtPath返回0),但文件是可删除的(在isDeletableFileAtPath返回1). 错误仍然存在,并且没有删除该文件。请发布用于检索路径的代码,好吗?您是否能够在设备上进行测试,以排除涉及模拟器应用程序路径的一些奇怪问题?不,我不能,我没有测试设备,只有XCode。我看不出代码中有任何明显错误,所以我不知道除了尝试在设备上测试它之外还有什么建议,如果你能找到的话。一个二手的iPodtouch就足够了,而且相当便宜。这是不是合适的objective-c?
NSFileManager *fileManager = [NSFileManager new];
    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]);
    }