title: NSString writeToFile with URL encoded string
body: <p>I have a Mac application that keeps it's own log file. It appends info to the file using NSString's writeToFile method. One of the things that it logs are URL's of web services that it is interacting with. To encode the URL, I'm doing this:</p>
  
  <pre><code>searchString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)searchString, NULL, (CFStringRef)@"!*'();:@&amp;=+$,/?%#[]", kCFStringEncodingUTF8 );
  </code></pre>
  
  <p>The app then appends searchString to the rest of the URL and writes it to the log file. Now the problem is that after adding that URL encoding line, nothing seems to be getting written to the file. The program functions as expected otherwise however. Removing the line of code above results in all of the correct information being logged to the file (removing that line is not an option because searchString must be URL encoded).</p>
  
  <p>Oh and I am using NSUTF8StringEncoding when writing the NSString to the file.</p>
  
  <p>Thanks for any help.</p>
  
  <p>EDIT: I know there's also a similar function to CFURLCreateStringByAddingPercentEscapes in NSString, but I've read that it doesn't always work. Can anyone shed some light on this if my original question cannot be answered? Thanks! (EDIT: same problem occurs when using <code>stringByAddingPercentEscapesUsingEncoding:</code>)</p>
  
  <p>EDIT 2: Here's the code that I'm using to append messages to the log file.</p>
  
  <pre><code>+(void)logText:(NSString *)theString{
      NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,NSUserDomainMask,YES) objectAtIndex:0];
      NSString *path = [docsDirectory stringByAppendingPathComponent:@"Folder/File.log"];
      NSString *fileContents = [[[NSString alloc] initWithContentsOfFile:path] autorelease];
      if([fileContents lengthOfBytesUsingEncoding:NSUTF8StringEncoding] &gt;= 204800){
          fileContents = @"";
      }
      NSString *timeStamp = [[NSDate date] description];
      timeStamp = [timeStamp stringByAppendingString:@": "];
      timeStamp = [timeStamp stringByAppendingString:theString];
      fileContents = [fileContents stringByAppendingString:timeStamp];
      fileContents = [fileContents stringByAppendingString:@"\n"];
      [fileContents writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];
  }
  </code></pre>