title: objective-c changing array contents inside for loop
body: <p>I have an issue that (I think) might have to do with scope, but I'm not sure. I'm trying to do something that I think should be simple, but I am getting a strange result, and I could truly use some advice. I would say I'm an early-objective-c programmer, but not a complete newb.</p>
  
  <p>I have written a function in objective-c that I would like to use to change the key-names in a mutable array of mutable dictionary objects. So, I want to pass in a mutable array of mutable dictionary objects, and return the same mutable array with the same dictionary objects, but with some of the key-names changed. Make sense?</p>
  
  <p>I have tried several log statements in this code, which seem to indicate that everything I'm doing is working, except when the for loop is finished executing (when I try to test the values in the temp array), the array appears to contain only the LAST element in the source array, repeated [source count] times. Normally, this would lead me to believe I'm not writing the new values correctly, or not reading them correctly, or even that my NSLog statements aren't showing me what I think they are. But might this be because of scope? Does the array not retain its changes outside of the for loop?</p>
  
  <p>I have put a fair amount of time into this function, and I have exhausted my bag of tricks. Can anyone help out?</p>
  
  <pre><code>-(NSMutableArray *)renameKeysIn:(NSMutableArray*)source {
  /*
  // Pre:
  // The source array is an array of dictionary items.
  // This method renames some of the keys in the dictionary elements, to make sorting easier later. 
  // - "source" is input, method returns a mutable array
   */
  
  // copy of the source array
  NSMutableArray *temp = [source mutableCopy];
  
  // a temporary dictionary object:
  NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
  
  // These arrays are the old field names and the new names
  NSMutableArray *originalField = [NSMutableArray arrayWithObjects:@"text", @"created_at",nil];
  NSMutableArray *replacedField = [NSMutableArray arrayWithObjects:@"title", @"pubDate", nil];
  
  // loop through the whole array
  for (int x =0; x&lt;[temp count]; x++) {
      // set the temp dictionary to current element
      [dict setDictionary:[temp objectAtIndex:x]]; 
  
      // loop through the number of keys (fields) we want to replace (created_at, text)... defined in the "originalField" array 
      for (int i=0; i&lt;[originalField count]; i++)
      {   
              // look through the NSDictionary item (fields in the key list)
              // if a key name in the dictionary matches one of the ones to be replaced, then replace it with the new one
              if ([dict objectForKey:[originalField objectAtIndex:i]] != nil) {
                  // add a new key/val pair: the new key *name*, and the old key *value* 
                  [dict setObject:[dict objectForKey:[originalField objectAtIndex:i]] 
                           forKey:[replacedField objectAtIndex:i]];
                  // remove the old key/value pair
                  [dict removeObjectForKey:[originalField objectAtIndex:i]];
              }// end if dictionary item not null
  
      }// end loop through keys (created_at, text)
  
      [temp replaceObjectAtIndex:x withObject:dict];
  
  }// end loop through array
  
  // check array contents
  for (int a=0; a&lt;[temp count]; a++){
      NSLog(@"Temp contents: ############ %@",[[temp objectAtIndex:a] objectForKey:@"pubDate"]);
  }
  
  return temp;    
  } // end METHOD
  </code></pre>