Invalid return value using NSFileManager createDirectoryAtPath:

I am working on a new feature for an existing iPhone application and would like to create several new directories in the local Documents folder of the application. I have successfully done this using the recommended method:

[NSFileManager createDirectoryAtPath:withIntermediateDirectories:attributes:error:] 

While reading the documentation for this method, I was intrigued by the return values ​​specified in the official announcement of Apple documentation :

Return value: YES, if the operation was successful or already exists , otherwise NO

Every time my application starts, I would like to make sure that the directories are installed correctly. I thought that a sensible way to do this would be to call the createDirectory method : each time it starts and use the value of the returned method. If for some reason the directory was missing, it will be created. If the directory has already been installed, the return value will be YES . You can use NO as a flag for additional recovery / recovery logic.

Unfortunately, I seem to get results that are not consistent with Apple documentation. This method returns NO if the directory already exists - when Apple reports that in this case it should return YES .

The following program demonstrates this behavior:

 #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSFileManager * fm = [NSFileManager defaultManager]; bool testDirectoryCreated = NO; testDirectoryCreated = [[NSFileManager defaultManager]createDirectoryAtPath: [NSString stringWithFormat:@"%@/%@",[fm currentDirectoryPath],@"TestDirectory"] withIntermediateDirectories: NO attributes: nil error: NULL]; NSLog(@"TestDirectory Created: %@\n", (testDirectoryCreated ? @"YES" : @"NO")); testDirectoryCreated = [[NSFileManager defaultManager]createDirectoryAtPath: [NSString stringWithFormat:@"%@/%@",[fm currentDirectoryPath],@"TestDirectory"] withIntermediateDirectories: NO attributes: nil error: NULL]; NSLog(@"TestDirectory Created: %@\n", (testDirectoryCreated ? @"YES" : @"NO")); [pool drain]; return 0; 

}

When the program is executed, it will print YES in the first call to createDirectory: the call and NO in the second call - when "TestDirectory" already exists.

Is this a mistake in the Apple documentation, or am I missing something?

Also, any other ideas for checking the integrity of my directory structure? Is there a simple "directory exists" method that I can call?

Thanks,

Tom

+4
source share
2 answers

If you want the convenience of checking if a directory with this method exists, you must pass TRUE to the withIntermediateDirectories: parameter.

This is indicated in the Apple documentation.

In addition, if you pass NO for this parameter, the directory should not exist at the time this call is made.

+4
source

It seems strange to me that the return value will be YES if the directory already exists. I would expect this value to be returned only to reflect success when creating the directory. This will match your results.

As for your other question, you can look at the ExistsAtPath: file and the ExistsAtPath: isDirectory : file under NSFileManager.

+1
source

All Articles