title: iPhone/Objective C: Can't delete a file
body: <p>In my application, I let the user record a sound clip and later, if the user chooses, I want him to be able to delete it.</p>
  
  <p>This is the code I use:</p>
  
  <pre><code>NSFileManager *fileManager = [NSFileManager defaultManager];
  NSError *error;
  NSLog(@"File exists: %d", [fileManager fileExistsAtPath:path]);
  NSLog(@"Is deletable file at path: %d", [fileManager isDeletableFileAtPath:path]);
  [fileManager removeItemAtPath:path error:&amp;error];
  if (error != nil)
  {
      NSLog(@"Error: %@", error);
      NSLog(@"Path to file: %@", path);
  }
  </code></pre>
  
  <p>The problem is that <code>fileExistsAtPath</code> and <code>isDeletableFileAtPath</code> return null and the <code>removeItemAtPath</code> doesn't work, and throws this error, </p>
  
  <blockquote>
    <p>Error: Error Domain=NSCocoaErrorDomain Code=4 UserInfo=0x391b7f0 "Operation could not be completed. (Cocoa error 4.)"</p>
  </blockquote>
  
  <p>The path has this form:</p>
  
  <pre class="lang-none prettyprint-override"><code>/Users/andrei/Library/Application%20Support/iPhone%20Simulator/User/Applications/5472B318-FA57-4F8D-AD91-7E06E9609215/Documents/1280913694.caf
  </code></pre>
  
  
  
  <p>There is a file there called <code>1280913694.caf</code>, but it doesn't pick it up. Does it have something to do with the way in which the path should be represented?</p>
  
  <p>The path works when playing the audio file with <code>AVAudioPlayer</code>.</p>
  
  <p>I've also changed the <code>%@</code> to <code>%d</code> for <code>fileExistsAtPath</code> and <code>isDeletableFileAtPath</code> and the answer is 0, which I suppose means FALSE.</p>
  
  <p>The name of the file is stored in a database, and the path to the file is retrieved with this method:</p>
  
  <pre><code>-(NSString *)returnFullPathToDirectory
  {
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      return documentsDirectory;
  }
  </code></pre>
  
  <p>After I get this value, I use it in the following code</p>
  
  <pre><code>NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
  </code></pre>