Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your solution is correct, though Apple includes an important note within <code>NSFileManager.h</code>:</p> <pre><code>/* The following methods are of limited utility. Attempting to predicate behavior based on the current state of the filesystem or a particular file on the filesystem is encouraging odd behavior in the face of filesystem race conditions. It's far better to attempt an operation (like loading a file or creating a directory) and handle the error gracefully than it is to try to figure out ahead of time whether the operation will succeed. */ - (BOOL)fileExistsAtPath:(NSString *)path; - (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory; - (BOOL)isReadableFileAtPath:(NSString *)path; - (BOOL)isWritableFileAtPath:(NSString *)path; - (BOOL)isExecutableFileAtPath:(NSString *)path; - (BOOL)isDeletableFileAtPath:(NSString *)path; </code></pre> <p>Essentially, if multiple threads/processes are modifying the file system simultaneously the state could change in between calling <code>fileExistsAtPath:isDirectory:</code> and calling <code>createDirectoryAtPath:withIntermediateDirectories:</code>, so it is superfluous and possibly dangerous to call <code>fileExistsAtPath:isDirectory:</code> in this context.</p> <p>For your needs and within the limited scope of your question it likely would not be a problem, but the following solution is both simpler and offers less of a chance of future issues arising:</p> <pre><code>NSFileManager *fileManager= [NSFileManager defaultManager]; NSError *error = nil; if(![fileManager createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:&amp;error]) { // An error has occurred, do something to handle it NSLog(@"Failed to create directory \"%@\". Error: %@", directory, error); } </code></pre> <p>Also note from <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html%23//apple_ref/doc/uid/20000305-SW5" rel="noreferrer">Apple's documentation</a>:</p> <blockquote> <p><strong>Return Value</strong></p> <p>YES if the directory was created, YES if createIntermediates is set and the directory already exists), or NO if an error occurred.</p> </blockquote> <p>So, setting <code>createIntermediates</code> to <code>YES</code>, which you already do, is a de facto check of whether the directory already exists.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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