Cocoa - NSFileManager removeItemAtPath not working

I'm trying to delete a file, but somehow nsfilemanager will not let me do this. I use the file in one line of code, but as soon as this action is completed, I want the file to be deleted. I registered an error code and a message, and I get an error code: 4 and a message:

"text.txt" could not be removed

Is there a way to fix this error “cleanly” (without any hacks) so that the apple accepts this application in its Mac App Store?

EDIT:

This is what I use:

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

Thank,

Kevin

+3
source share
6 answers

4 NSNoSuchFileError. , , , , . , , , .

, .

+11

swift. - fileManager.removeItemAtPath , Manager.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)
+3

, . .

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

.

+2

NSString *str = [outputFieldURL path];

NSString *str = [outputFieldURL absoluteString];

"removeItemAtPath:" . URL-, -removeItemAtURL:

+2

, , NSFileManager. Sandboxing.

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

. FileManager (,), , .

.

( 5 ) ; -)

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;
}
0

Source: https://habr.com/ru/post/1783996/


All Articles