[英]Cocoa - NSFileManager removeItemAtPath Not Working
我試圖刪除一個文件,但不知何故nsfilemanager不允許我這樣做。 我在一行代碼中使用該文件,但是一旦運行了該操作,我希望刪除該文件。 我已經記錄了錯誤代碼和消息,我得到錯誤代碼:4和消息:
"text.txt" could not be removed
有沒有辦法“干凈地”(沒有任何黑客)修復此錯誤,以便蘋果將接受這個應用程序到他們的Mac App Store?
編輯:
這就是我正在使用的:
[[NSFileManager defaultManager] removeItemAtPath:filePath error:NULL];
謝謝,
凱文
錯誤代碼4似乎是NSNoSuchFileError 。 如果您要刪除的文件確實存在,那么您的路徑錯誤。 如果您希望我們確切地告訴您如何弄錯路徑,則需要發布一些代碼。
如果該文件不存在,則可以忽略該錯誤。
我在swift中有類似的問題。由於某種原因,fileManager.removeItemAtPath不起作用,我將fileManager.removeItemAtPath(filePath)更改為fileManager.removeItemAtURL(fileURL),它工作正常。
let fileManager = NSFileManager()
let documentsFolderUrl = fileManager.URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false, error: nil)
let soundURL = documentsFolderUrl!.URLByAppendingPathComponent(recording.path)
let stringTrimmedFilePath = "trimmed_\(recording.path)"
let trimmedSoundURL = documentsFolderUrl!.URLByAppendingPathComponent(stringTrimmedFilePath)
var error: NSError?
fileManager.removeItemAtURL(trimmedSoundURL, error: &error)
你的路徑不正確
使用以下內容
NSString *str = [outputFieldURL path];
代替
NSString *str = [outputFieldURL absoluteString];
方法“removeItemAtPath:”需要文件的本地路徑,如果要使用url刪除,則應使用-removeItemAtURL:
首先,您需要選擇文檔目錄的路徑,然后才能刪除該文件。 只刪除語句是不夠的。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *databaseFile = [documentsDirectoryPath stringByAppendingPathComponent:@"text.txt"];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:databaseFile error:NULL];
用它來解決你的問題。
當你使用NSFileManager時,我只是想出了一些非常重要的東西。 您必須了解App Sandboxing。
let documentDirectory = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true)[0]
此行返回應用程序沙箱的路徑文檔目錄。 在文檔目錄中使用FileManager創建文件時(例如)不保存完整文件路徑,只保存當前文檔目錄中的路徑。
您將能夠重新創建所創建文件的完整路徑。
希望(5年后)幫助開發人員;-)
默認方法AFNetworking 3.0不刷新您的下載程序文件。
如果你想在Objective-C + iOS9.0中重寫這個文件,你需要這樣做:
- (NSURLSessionDownloadTask *) downloadDocsFromUrl:(NSString *) url withSuccesBlock:(DocModelBlock) docModelBlock withErrorBlock:(ErrorBlock) errorBlock {
NSURL *URL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [self.sessionManager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSURL *documentsDirectoryURL = [fileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:&error];
if ([httpResponse statusCode] == 200) {
NSURL *urlPath = [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
if ([fileManager fileExistsAtPath:urlPath.path]) {
[fileManager removeItemAtPath:urlPath.path error:&error];
}
}
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
if (error) {
errorBlock(error);
} else {
docModelBlock(filePath);
}
}];
[downloadTask resume];
return downloadTask;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.