title: Attempt to rename file in Documents directory has error "file:/..."
body: <p>Below is the code I have written to change the name of any file stored in an iOS device "<code>Documents</code>" directory. </p>
  
  <p>When renaming the file using this code, the prefix resolves to...</p>
  
  <pre><code>"file:/localhost/private/var/mobile/Applications/.../Documents/"
  </code></pre>
  
  <p>instead of the correctly formatted...</p>
  
  <pre><code>"file://localhost/private/var/mobile/Applications/.../Documents/" 
  </code></pre>
  
  <p>I am losing a slash, and so the rename method fails!!!</p>
  
  <p>I have rewritten this for both <code>NSString</code> and <code>NSURL</code> formats, and both methods present the identical error. I have tested this using the iOS Simulator and on device, and both tests present the identical error.</p>
  
  <p>I suspect I am overlooking something simple, but cannot figure what it is - any help please?</p>
  
  <pre><code>- (void)alterFileNameOrExtension {
      NSError *errorMove = nil;
      NSError __autoreleasing *error = nil;
  
  //note below - entity attribute document.dStringURL has a data type NSString
  
      NSString *file = [document.dStringURL lastPathComponent];
      NSString *fileName = [file stringByDeletingPathExtension];
      NSString *fileExtension = [file pathExtension];
      NSString *fileNameNew = nil;
  
  //note below - self.tempFileComponent, self.tempFileName &amp; self.tempFileExtension
  //note below - are set in the (IBAction) method textFieldDidEndEditing: 
  //note below - self.tempFileComponent determines whether the user is attempting to change the file name or the file extension
  
      if (self.tempFileComponent == 1) {
          fileNameNew = [[self.tempFileName stringByAppendingPathExtension:fileExtension] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
      } else if (self.tempFileComponent == 2) {
          fileNameNew = [fileName stringByAppendingPathExtension:self.tempFileExtension];
      } else {
          NSLog(@"%@ - %@ - attempt to determine tempFileComponent has (possible undefined) E~R~R~O~R: %@_", self.className, NSStringFromSelector(_cmd), error.localizedDescription);
      }
  
      NSString *filePath = [document.dStringURL stringByDeletingLastPathComponent];
      NSString *filePathNew = [filePath stringByAppendingPathComponent:fileNameNew];
  
      BOOL move = [[NSFileManager defaultManager] moveItemAtPath:document.dStringURL toPath:filePathNew error:&amp;errorMove];
      if (!move) {
          //handle error
      } else {
          //complete process
      }
  }
  </code></pre>
  
  <h1>#</h1>
  
  <p>With assistance from @Mario, I have adjusted my code and include the working version below for the benefit of others...</p>
  
  <h1>#</h1>
  
  <pre><code>NSError *errorMove = nil;
  NSError __autoreleasing *error = nil;
  
  NSString *file = [document.dStringURL lastPathComponent];
  NSString *fileName = [file stringByDeletingPathExtension];
  NSString *fileExtension = [file pathExtension];
  NSString *fileNameNew = nil;
  
  if (self.tempFileComponent == 1) {
      fileNameNew = [self.tempFileName stringByAppendingPathExtension:fileExtension];
  } else if (self.tempFileComponent == 2) {
      fileNameNew = [fileName stringByAppendingPathExtension:self.tempFileExtension];
  } else {
      NSLog(@"%@ - %@ - attempt to determine tempFileComponent has (possible undefined) E~R~R~O~R: %@_", self.className, NSStringFromSelector(_cmd), error.localizedDescription);
  }
  
  NSURL *fileURLOld = [NSURL URLWithString:document.dStringURL];
  NSURL *fileURLPrefix = [fileURLOld URLByDeletingLastPathComponent];
  NSURL *fileURLNew = [fileURLPrefix URLByAppendingPathComponent:fileNameNew];
  
  BOOL move = [[NSFileManager defaultManager] moveItemAtURL:fileURLOld toURL:fileURLNew error:&amp;errorMove];
  if (!move) {
      //handle error
  } else {
      //complete process
  }
  </code></pre>