Improper Return Value using NSFileManager createDirectoryAtPath:
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:
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
If you'd like to get the convenience to also check if the directory exists with this method, you must pass TRUE to the withIntermediateDirectories: parameter.
This is stated in Apple's documentation
In addition, if you pass NO for this parameter, the directory must not exist at the time this call is made.
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.
精彩评论