4

我正在尝试删除文件,但不知何故 nsfilemanager 不允许我这样做。我确实在一行代码中使用了该文件,但是一旦运行了该操作,我希望删除该文件。我已经记录了错误代码和消息,我得到错误代码:4 和消息:

"text.txt" could not be removed

有没有办法“干净”地修复这个错误(没有任何黑客),以便苹果将这个应用程序接受到他们的 Mac App Store 上?

编辑:

这就是我正在使用的:

[[NSFileManager defaultManager] removeItemAtPath:filePath error:NULL];

谢谢,

凯文

4

6 回答 6

13

Error code 4 seems to be NSNoSuchFileError. If the file you want to delete really exists, then you have got the path wrong. You'll need to post some code if you want us to tell you exactly how you got the path wrong.

If the file doesn't exist, you can ignore the error.

于 2011-01-07T09:37:42.043 回答
3

我在 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)
于 2015-03-09T19:47:59.320 回答
2

首先,您需要选择文档目录的路径,然后您可以删除该文件。仅删除语句是不够的。

   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];

用它来解决你的问题。

于 2011-01-07T04:59:52.540 回答
2

你的路径不正确

使用以下

NSString *str = [outputFieldURL path];

代替

NSString *str = [outputFieldURL absoluteString];

“removeItemAtPath:”方法需要文件的本地路径,如果你想使用url删除,你应该使用-removeItemAtURL:

于 2013-11-05T07:29:04.690 回答
0

默认方法 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;
}
于 2018-03-27T14:54:30.313 回答
0

当你使用 NSFileManager 时,我只是想出了一些非常重要的事情。您必须了解 App Sandboxing。

let documentDirectory = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true)[0]

此行返回您的应用沙盒的路径文档目录。当您在文档目录中使用 FileManager 创建文件时(例如),不要保存完整的文件路径,而只保存当前文档目录的路径。

您将能够重新创建已创建文件的完整路径。

希望(5 年后)帮助开发人员 ;-)

于 2016-09-02T15:07:59.223 回答