title: Objective-C - How to write an array to a file?
body: <p>I have a text file that I can scan (NSScanner) and tag. The results are stored in an array. The structure is two strings, one for english and one for greek.</p>
  
  <p>I want to write an output file that maintains the array structure. I presume I can create a plist file for this purpose.</p>
  
  <p>However, I'm stuck and need help to create this file.</p>
  
  <p>I have a test whether the file was created, but I get this result:</p>
  
  <pre><code>outgoingWords.count: 442
  2014-08-12 17:54:17.369 MyScanner[97350:2681695] *** Assertion failure in -[ViewController checkArray], /Users/david/Desktop/Word Scanning/MyScanner/MyScanner/ViewController.m:98
  2014-08-12 17:54:17.373 MyScanner[97350:2681695] Failed to set (contentViewController) user defined inspected property on (NSWindow): writeToFile failed
  </code></pre>
  
  <p>The code I'm using so far is as follows:</p>
  
  <pre><code>-(void)checkArray {
  
      //do stuff to verify the array       
  
      long i = outgoingWords.count;
      NSString *tempString = [NSString stringWithFormat:@"%li", i];
      NSLog(@"outgoingWords.count: %@", tempString); //442
  
      NSArray *tempArray2 = [outgoingWords copy];
      NSString *path = [[NSBundle mainBundle] pathForResource:@"/Users/David/Desktop/alfa2" ofType:@"plist"];
  
      BOOL success = [tempArray2 writeToFile:path atomically:YES];
      NSAssert(success, @"writeToFile failed");
    }
  </code></pre>
  
  <p>Could someone either identify what I'm missing, or point me to an existing answer I can use (I've looked)..</p>
  
  <p>Many thanks..</p>
  
  <p>edit. I've also tried the approach in <a href="https://stackoverflow.com/questions/1286212/how-to-convert-nsarray-to-nsdata">this SO question</a>. But get the same result.</p>
  
  <pre><code> NSString *path = [[NSBundle mainBundle] pathForResource:@"/Users/David/Desktop/alfa2" ofType:@"plist"];
  
      NSString *error;
      NSData *data = [NSPropertyListSerialization dataFromPropertyList:tempArray2 format:NSPropertyListBinaryFormat_v1_0 errorDescription:&amp;error];
  
      BOOL success = [NSKeyedArchiver archiveRootObject:data toFile:path];
      NSAssert(success, @"archiveRootObject failed");
  </code></pre>