繁体   English   中英

Cocoa - NSFileManager removeItemAtPath不工作

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM