Cocoa - NSFileManager removeItemAtPath Not Working

空扰寡人 提交于 2019-12-01 20:23:54

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.

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)

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.

Jasmeet

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:

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

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!