I still haven't gotten the full picture by reading all the answers above. The layman lesson that I did below helped me understand what was going on. Just put it there if it helps other newbies.
Suppose you have the following
@interface Class X -(void) methodX:(NSMutableArray *)array; @end
In some other part of the code, you have the following sequence
ClassX *objectX = [[ClassX alloc] init]; NSMutableArray *arrayXX = [@[@(1), @(2)] mutableCopy]; //What is stored in arrayXX is the address in the heap at which the NSMutableArray object starts, lets call this address ZZZ //array starting at address ZZZ in the heap now contains NSNUmbers @1,@2 [objectX methodX:array]
When you call [objectX methodX:array] , what the method gets is a copy of array . Since the array contains an address (i.e., Pointer), the copy is special in that what is received is another variable with the ZZZ address in it.
So, if methodX does [array removeObjectAtIndex:0] , then the object starting with the ZZZ address is exposed (now it contains only one NSNUmber @ (2)). Thus, when the method returns, the original array is also exposed.
Suppose that instead of method X does array = [@[@(2)] mutableCopy]; the original array will not be affected. This is because you have not entered the ZZZ address and changed something. Instead, you copied ZZZ in the copy obtained by the method to another YYY address. The YYY address is the start of an NSMUtableArray with one NSNUmber @ (2) element. The ZZZ source address still contains an NSMUtableArray with two elements. @ (1) and @ (2). That way, when the method returns, the original array does not change.
Smart Home Aug 12 '16 at 6:35 2016-08-12 06:35
source share