【发布时间】:2010-08-04 10:39:27
【问题描述】:
在我的应用程序中,我让用户录制声音片段,然后,如果用户选择,我希望他能够删除它。
这是我使用的代码:
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 Domain=NSCocoaErrorDomain Code=4 UserInfo=0x391b7f0“操作无法完成。(Cocoa 错误 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];
【问题讨论】:
-
请将
-removeItemAtPath前的两个%@改成%d再运行。 -
注意file://前缀必须去掉,即转换为NSURL并获取.path属性(确实是上面的路径)
标签: objective-c iphone file file-io