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