Note that there are some explanatory texts on larger screens.

plurals
  1. POImproper Return Value using NSFileManager createDirectoryAtPath:
    primarykey
    data
    text
    <p>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:</p> <pre><code>[NSFileManager createDirectoryAtPath:withIntermediateDirectories:attributes:error:] </code></pre> <p>When reading the documentation for this method, I was intrigued by return values listed in Apple's official <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/createDirectoryAtPath:withIntermediateDirectories:attributes:error:" rel="nofollow noreferrer">documentation</a>:</p> <blockquote> <p>Return Value: YES if the operation was successful <strong>or already exists</strong>, otherwise NO</p> </blockquote> <p>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 <strong>createDirectory:</strong> 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 <strong>YES</strong>. A <strong>NO</strong> return value could then be used as a flag for additional recovery/repair logic.</p> <p>Unfortunately, I appear to be getting results inconsistent with Apple's documentation. The method is returning <strong>NO</strong> if the directory already exists - when Apple's docs say it should return <strong>YES</strong> in this case.</p> <p>The following program demonstrates this behavior:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; 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; </code></pre> <p>}</p> <p>When the program executes, it will print <strong>YES</strong> on the first createDirectory: call, and <strong>NO</strong> on the second call - when "TestDirectory" already exists.</p> <p>Is this an error in Apple's documentation, or am I missing something?</p> <p>Also, any other ideas for just validating the integrity of my directory structure? Is there a simple "directory exists" method I can call?</p> <p>Thanks,</p> <p>Tom</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload