在我的應用程序中,我讓用戶錄製聲音片段,之後如果用戶選擇,我希望他能夠刪除它。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