title: How do i create a file in the default documents directory in an iOS app
body: <p>I'm writing an app that needs to store some persistence information in file but i'm having trouble debugging the createFileAtPath:contents:attributes: function of NSFileManager, i have reduced the problem to the below piece of code.</p>
  
  <pre><code>NSFileManager *filemgr;
  filemgr = [NSFileManager defaultManager];
  NSString * fileName = @"newfile.txt";
  
  NSArray *paths = NSSearchPathForDirectoriesInDomains
  (NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  
  NSString *filePath = [documentsDirectory stringByAppendingPathComponent: fileName];
  NSLog(@"full path name: %@", filePath);
  
  // check if file exists
  if ([filemgr fileExistsAtPath: filePath] == YES){
      NSLog(@"File exists");
  
  }else {
      NSLog (@"File not found, file will be created");
      if (![filemgr createFileAtPath:filePath contents:nil attributes:nil]){
          NSLog(@"Create file returned NO");
      }
  }
  </code></pre>
  
  <p>Since the function doesn't take an NSError object i'm having trouble finding out why exactly it is returning false.</p>
  
  <p>From other examples i have seen here <a href="https://stackoverflow.com/questions/5619719/write-a-file-on-ios">Write a file on iOS</a> and here <a href="https://stackoverflow.com/questions/7011070/how-to-programmatically-create-an-empty-m4a-file-of-a-certain-length-under-ios/10159799#10159799">How to programmatically create an empty .m4a file of a certain length under iOS using CoreAudio on device?</a> it should be ok to pass nil to both the contents and attributes parameters. I've tried specifying both parameters and receive the same result.</p>
  
  <p>I'm thinking this may be a permissions issue but i'm unsure if the below code is the correct way to check permissions on a directory.</p>
  
  <pre><code>if ([filemgr isWritableFileAtPath:documentsDirectory] == YES){
      NSLog (@"File is readable and writable");
  }
  </code></pre>