When you have an NSArray and you want to evaluate and change the elements, you can’t change the array from inside the loop. So, you create a mutable copy that can be changed.
code example:
NSMutableArray *bin = [NSMutableArray arrayWithObjects:@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", nil];
NSMutableArray *list = [NSMutableArray arrayWithObjects:@"a1", @"b2", @"c3", @"e4", nil];
NSMutableArray *listHolder = list; // can't mutate 'list' within loop so create a holder
for (int i = 0; i < [list count]; i++) {
[listHolder replaceObjectAtIndex:i withObject:[bin objectAtIndex:i]];
}
What is that second array listHolder called? I mean, what term is used to refer to an array in this context.
In this context
listHolderwould be called a copy.Your code has a bug though. This line is not actually making a copy, it is only letting
listHolderandlistboth reference the same array object:This would be an actual copy:
Make sure that you use
mutableCopyand not justcopyif you want the copy to be mutable. Thecopymethod will return immutable variants on all mutable classes such asNSMutableSet,NSMutableDictionary, and so forth.Also as others have noted it is only inside the
for (item in collection)loop that the enumerated collection can not be mutated. In a normalfor (;;)mutation is perfectly ok, but can lead to strange result if the number of items in the collection changes.