I am trying to change the contents of an array while enumeration. To avoid the warning ,”the array was mutated while being enumerated” i made a copy of the array and done the autorelease like this-
int iKeyArrayCount=0;
for(NSString *keyEntity in [[keyArray copy]autorelease])
{
[keyArray replaceObjectAtIndex:iKeyArrayCount withObject:[keyEntity stringByReplacingOccurrencesOfString:@"\"" withString:kMPVTBlankString]];
[keyArray replaceObjectAtIndex:iKeyArrayCount withObject:[keyEntity stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
iKeyArrayCount++;
}
My confusion is regarding the enumeration over that copy of keyArray. While enumeration is it like , a copy of keyArray is formed each time the for loop executes? Or only one copy of that keyArray is formed during the whole process of enumeration.
In that code snippet
[[keyArray copy] autorelease]is executed once only, and the result is used at the object being iterated.In other words, “only one copy of that keyArray is formed during the whole process of enumeration” is correct.