views:

132

answers:

1

Hi Everyone,

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

[NSFileManager createDirectoryAtPath:withIntermediateDirectories:attributes:error:]

When reading the documentation for this method, I was intrigued by return values listed in Apple's official documentation: http://bit.ly/aNzT57:

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

Each time my application starts up, I would like to ensure that the directories are properly in place. I thought a clever way of doing this would be to call the createDirectory: method on each start and take advantage of the method's return value. If the directory was missing for some reason, it would be created. If the directory was already in place, the return value would still be YES. A NO return value could then be used as a flag for additional recovery/repair logic.

Unfortunately, I appear to be getting results inconsistent with Apple's documentation. The method is returning NO if the directory already exists - when Apple's docs say it should return YES in this case.

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 executes, it will print YES on the first createDirectory: call, and NO on the second call - when "TestDirectory" already exists.

Is this an error in Apple's documentation, or am I missing something?

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

Thanks,

Tom

+1  A: 

It does seem odd to me that the return value would be YES, if the directory already exists. I would have expected this return Value to only reflect success on creating the dir. Which would be consistent to your returns.

As to your other question, you may want to look at fileExistsAtPath: and fileExistsAtPath:isDirectory: under the NSFileManager.

markhunte
Yes, it does seem odd that the method would return yes in the "already exists" case, but it seemed convenient for what I was doing. Apple's documentation seems to contradict itself - earlier in the method description, it states that the path at which you create the directory must not yet exist, yet its description of return values seems to indicate otherwise. Thanks for the other suggestions !
thauburger