title: Writing array contents to a file
body: <p>I've struggled with this all day using answers from here, but I cannot find a solution that is working for me, so I thought I'd ask for help:</p>
  
  <p>I have an array of objects that I've extracted from an entity.</p>
  
  <p>All I want to do is to write the objects to a file.</p>
  
  <p>What I've come up with so far is this:</p>
  
  <pre><code>    NSLog(@" Exported Records: %i", [exportArray count]); 
      // the count here is 4 records.
      //Each record has about 8 elements. 
  //I'm just trying to get this working with the first two elements right now
  
          NSString *writeString = nil;
          NSError *error = nil;
          int i = 0;
          NSString *key = nil;
          NSString *tempString = nil;
          for (i=0; i&lt;[exportArray count]; i++)
          {
              tempString = [tempString stringByAppendingString: @" \n "];
  
              for (int j=0; j&lt;3; j++)
              {
                  if (j == 0)
                  {
                      key = [[exportArray objectAtIndex:i]  valueForKey:@"title"];
                      //[NSString stringWithFormat:tempString2, [[exportArray objectAtIndex:i]  valueForKey:@"title"]];
                  }
                  if (j == 1)
                  {
                      key = [[exportArray objectAtIndex:i]  valueForKey:@"username"];
                      //[NSString stringWithFormat:tempString2, [[exportArray objectAtIndex:i]  valueForKey:@"username"]];
                  }
                  writeString = [NSString stringWithFormat:tempString, key];
              }
          }
  
          // Write to the file
          [writeString writeToFile:dataFile atomically:YES
                      encoding:NSUTF8StringEncoding error:&amp;error];
          if (error)
          {
              NSLog(@"%@", error);
          }
  </code></pre>
  
  <p>Right now, all I'm getting the the last item, so I'm overwriting the string. But, there must be a better method to achieve this. Please let me know if there is another answer here matches my question, or please, post some ideas.</p>
  
  <p>UPDATE</p>
  
  <p>When I log the exportArray, I get this:</p>
  
  <pre><code>"&lt;ItemEntity: 0x1ddf6560&gt; (entity: ItemEntity; id: 0x1ddf46d0 &lt;x-coredata://78FBC4A5-AE1A-4344-98AB-978126457D96/ItemEntity/p2&gt; ; data: &lt;fault&gt;)",
      "&lt;ItemEntity: 0x1ddf6800&gt; (entity: ItemEntity; id: 0x1ddf46e0 &lt;x-coredata://78FBC4A5-AE1A-4344-98AB-978126457D96/ItemEntity/p2051&gt; ; data: &lt;fault&gt;)",
      "&lt;ItemEntity: 0x1ddf6860&gt; (entity: ItemEntity; id: 0x1ddf45d0 &lt;x-coredata://78FBC4A5-AE1A-4344-98AB-978126457D96/ItemEntity/p3075&gt; ; data: &lt;fault&gt;)",
      "&lt;ItemEntity: 0x1ddf68c0&gt; (entity: ItemEntity; id: 0x1ddf45e0 &lt;x-coredata://78FBC4A5-AE1A-4344-98AB-978126457D96/ItemEntity/p5124&gt; ; data: &lt;fault&gt;)"
  )
  </code></pre>
  
  <p>When I log the actual values:</p>
  
  <pre><code>NSLog(@" Exported titles: %@", [exportArray valueForKey:@"title"]);
  NSLog(@" Exported usernames: %@", [exportArray valueForKey:@"username"]);
  </code></pre>
  
  <p>I get correct results. I just don't know how to tie these and the other attributes together..</p>
  
  <pre><code>  Exported titles: (
      1,
      2,
      4,
      6
  )
    Exported usernames: (
      ellis,
      david,
      bea,
      ian
  )
  </code></pre>