-1

My app needs to create a subdirectory of the Documents directory. The code below returns the directory URL. First time through it creates the directory, but fails next time because fileExistsAtPath: claims the directory still doesn't exist. I know it exists though because I have set the bundle to make files visible in iTunes and I can see it in iTunes. Any idea what I'm doing wrong?

- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager]   URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

- (NSURL *)applicationSubDirectory
{
    NSFileManager *manager = [NSFileManager defaultManager];
    NSURL *docDir = [self applicationDocumentsDirectory];
    docDir = [docDir URLByAppendingPathComponent:@"Sub" isDirectory:YES];
    NSString *docDirPath = [docDir absoluteString];
    BOOL isFolder = YES;

    if(!([manager fileExistsAtPath:docDirPath isDirectory:&isFolder] && isFolder))    {
        NSError *error = nil;
        BOOL ret = [manager createDirectoryAtURL:docDir withIntermediateDirectories:NO attributes:nil error:&error];
        if(!ret) {
            NSLog(@"ERROR app support: %@", error);
            abort();
        }
    }
    return docDir;
}
zaph
  • 111,848
  • 21
  • 189
  • 228
easiwriter
  • 73
  • 1
  • 8
  • 1
    For starters `NSLog()` `docDirPath` and add that to the question. Also provide the error message. – zaph Jan 22 '15 at 16:55
  • The error test is purely for testing, not production code. docDirPath is used as an argument to fileExistsAtPath:. The code generates cocoa error 4. – easiwriter Jan 22 '15 at 18:47
  • I have explained what the code is trying to do. I am simply asking for someone to explain why fileExistsAtPath: returns false when the directory in question already exists. Someone who has experience with NSFileManager might be able to do that. What other information is required? – easiwriter Jan 22 '15 at 20:13
  • I know my code is broken, which is why I've posted the request for help. It is probably something about the rules applying to directories that I have misunderstood. I do not know what additional information to provide. Perhaps you might suggest something that would help you. – easiwriter Jan 22 '15 at 21:22
  • Look at the first comment. – zaph Jan 22 '15 at 21:35

1 Answers1

2

You want to use path instead of absoluteString The former returns the full url absolute path, something like "file:///..." while the latter only returns the file path.

NSString *docDirPath = [docDir path];

So, since you're looking for a file with the path "file:///...." which will never exist, the check always fails. When you go to create the directory, you create it with the URL, so it works.

David Berry
  • 40,941
  • 12
  • 84
  • 95
  • Sad the OP would not provide the NSLog() of `docDir` and `docDirPath`, he might have figured it out himself.. – zaph Jan 23 '15 at 00:26
  • Thank you David. If Zaph already knew the answer then it would have been helpful if he'd said. I'd already looked at the data and missed the obvious. It happens sometimes. – easiwriter Jan 23 '15 at 06:52