title: How to save text/data in cocoa/xcode to a file?
body: <p>i'm sorry for the obvious question, but is there something i should be careful about when writing data to a file, because my program can read the data it just wrote, while the program is running, but the moment i stop it, the files become empty</p>
  
  <p>tried using NSFileHandle, in order to write data with it, and close the file later, didn't help... currently, i'm using:</p>
  
  <pre><code>NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:newArray];
  [encodedObject writeToFile:string atomically:YES];
  </code></pre>
  
  <p>and no matter what i do, i can't get the simplest NSString to stay in the file permanently</p>
  
  <p>what do i do?</p>
  
  <p>Thanks @LetzFlow, but it didn't solve it just yet.</p>
  
  <p>Now, i'm using:</p>
  
  <pre><code>NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  
  string = [NSString stringWithFormat:@"%d.rtf",fileName];    
  NSString *file = [documentsDirectory stringByAppendingPathComponent:string];
  
  //NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:file];
  NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:newArray];
  
  [encodedObject writeToFile:file atomically:YES];
  //[fileHandle writeData:encodedObject];
  //[fileHandle closeFile];
  NSLog(@"%@",[NSString stringWithContentsOfFile:file encoding:NSStringEncodingConversionAllowLossy error:nil]);
  </code></pre>
  
  <p>to serialize an array of objects, and it the NSLog shows a valid array. Yet, when I look at the file later, or try to unserialize the array (like this):</p>
  
  <pre><code>NSString *stringX = [NSString stringWithFormat:@"%d.rtf",fileName];
  NSData *encodedObjectX = [NSData dataWithContentsOfFile:stringX];
  NSArray *newArrayX = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObjectX];
  TurnigButton *button = [[newArrayX objectAtIndex:1] objectAtIndex:5];
  NSLog(@"%d", button.idNum);
  </code></pre>
  
  <p>it just prints (null). (when they execute one after the other in a single run, it unserializes just fine)</p>
  
  <p>I appreciate the help.</p>