在我的应用程序中,我让用户录制声音片段,之后如果用户选择,我希望他能够删除它。iPhone /目标C:无法删除文件
这是我使用的代码:
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和removeItemAtPath
不工作,并抛出这个错误,
Error: Error Domain=NSCocoaErrorDomain Code=4 UserInfo=0x391b7f0 "Operation could not be completed. (Cocoa error 4.)"
的路径有此表格:
/Users/andrei/Library/Application%20Support/iPhone%20Simulator/User/Applications/5472B318-FA57-4F8D-AD91-7E06E9609215/Documents/1280913694.caf
有一个文件叫做1280913694.caf
,但它没有选择它。它是否与路径应该被表示的方式有关?
播放与AVAudioPlayer
音频文件时路径工作。
我也改变了%@
到%d
为fileExistsAtPath
和isDeletableFileAtPath
,答案是0,我想就是假。
文件的名称被存储在数据库中,并以文件的路径是用这种方法检索:
-(NSString *)returnFullPathToDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}
后,我得到这个值,我用它下面的代码
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
请将'-removeItemAtPath'前面的'%@'更改为'%d'并再次运行。 – kennytm 2010-08-04 10:42:09
注意file://前缀必须被移除,即转换为NSURL并获得.path属性(实际上路径如上) – ThomasRS 2011-08-18 15:06:49