1

I'm trying to use the method seen in the solution here to delete all the files in the iPhone documents directory for the application I'm writing. I've made some minor changes to the code in the solution in order to pass in the string location of the documents directory. My version of the code is the following:

NSString *directory = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] absoluteString];
NSLog(@"%@", directory);
NSError *error = nil;
NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directory error:&error];
if (error == nil) {
    for (NSString *path in directoryContents) {
        NSString *fullPath = [directory stringByAppendingPathComponent:path];
        BOOL removeSuccess = [[NSFileManager defaultManager] removeItemAtPath:fullPath error:&error];
        if (!removeSuccess) {
            // Error handling
        }
    }
} else {
    // Error handling
    NSLog(@"%@", error);
}

When I attempt to run this however, the setting of directoryContents fails due to what's being passed being interpreted as a non-existent directory. Specifically, the two NSLog() statements I have put in the code returns the following:

2013-04-22 11:48:22.628 iphone-ipcamera[389:907] file://localhost/var/mobile/Applications/AB039CDA-412B-435A-90C2-8FBAADFE6B1E/Documents/

2013-04-22 11:48:22.650 iphone-ipcamera[389:907] Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x1d5232c0 {NSUnderlyingError=0x1d54c420 "The operation couldn’t be completed. No such file or directory", NSFilePath=file://localhost/var/mobile/Applications/AB039CDA-412B-435A-90C2-8FBAADFE6B1E/Documents/, NSUserStringVariant=(

    Folder

)}

As far as I can see the path being printed to the NSLog looks correct so I'm not sure what I'm doing wrong. Can anyone point out to me where my mistake is? Thank you much!

1 Answer 1

8

Your code to get the value of directory isn't quite right. You want:

NSURL *directoryURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSString *directory = [directoryURL path];

Calling absoluteString on NSURL gives you a file URL. You don't want a file URL, you want the file URL converted to a file path. This is what the path method does.

Another way is:

NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
3
  • Victory. Thank you sir. Works as described and I appreciate the explanation as to why my previous method was not working.
    – golmschenk
    Apr 22, 2013 at 16:28
  • Even better, call -[NSFileManager contentsOfDirectoryAtURL:…] and avoid using paths entirely Apr 24, 2013 at 22:13
  • Using path instead of absoluteString fixed my problem!
    – Fogh
    Mar 5, 2014 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.