Cocoa - NSFileManager removeItemAtPath 不工作

发布于 2024-10-10 22:57:23 字数 384 浏览 3 评论 0原文

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

"text.txt" could not be removed

有没有办法“干净”地修复此错误(无需任何黑客攻击),以便苹果在其 Mac App Store 上接受此应用程序?

编辑:

这就是我正在使用的:

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

谢谢,

凯文

I am trying to delete a file, but somehow nsfilemanager will not allow me to do so. I do use the file in one line of code, but once that action has been ran, I want the file deleted. I have logged the error code and message and I get error code: 4 and the message:

"text.txt" could not be removed

Is there a way to fix this error "cleanly" (without any hacks) so that apple will accept this app onto their Mac App Store?

EDIT:

This is what I am using:

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

Thanks,

kevin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

复古式 2024-10-17 22:57:23

错误代码 4 似乎是 NSNoSuchFileError。如果你要删除的文件确实存在,那么你的路径就错了。如果您希望我们准确地告诉您路径错误的原因,您需要发布一些代码。

如果该文件不存在,您可以忽略该错误。

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.

青朷 2024-10-17 22:57:23

我在 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)

I had a similar problem in swift.For some reason fileManager.removeItemAtPath did't work and I changed fileManager.removeItemAtPath(filePath) to fileManager.removeItemAtURL(fileURL) and it works fine.

    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)
夕色琉璃 2024-10-17 22:57:23

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

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

用它来解决你的问题。

First you need to pick the path for the document directory then you can delete the file.
Only remove statement is not sufficient.

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

use this for solving your problem.

未央 2024-10-17 22:57:23

您的路径不正确

使用以下内容

NSString *str = [outputFieldURL path];

而不是

NSString *str = [outputFieldURL absoluteString];

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

Your path is incorrect

Use the following

NSString *str = [outputFieldURL path];

instead of

NSString *str = [outputFieldURL absoluteString];

The method "removeItemAtPath:" need the local path of file, If you want to remove using url, you should use -removeItemAtURL:

心房的律动 2024-10-17 22:57:23

我只是从使用 NSFileManager 时非常重要的事情中发现了这一点。您必须了解应用程序沙箱。

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

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

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

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

I just figure it out of something very important when you use NSFileManager. You have to be aware of App Sandboxing.

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

This line return the path document directory of your app sandboxed. When you create a file with FileManager in your document directory (for e.g) don't save the full file path but only the path from the current document directory.

You'll be able to recreate the full path of your created file.

Hope (after 5 years) help over developers ;-)

单身狗的梦 2024-10-17 22:57:23

默认方法 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;
}

Default method AFNetworking 3.0 don't refresh that you downloader files.

If you want rewrite this file in Objective-C +iOS9.0, you need do that:

- (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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文