I am trying to understand something that i thought i know.
if in class A i do :
-(NSMutableArray*)setArray:(NSMutableArray*) array1
{
//some calculations on array 1
return array1.
}
than in classB i :
ClassA *instanceA = [[ClassA alloc]init] ;
ClassC *instanceC = [[ClassC alloc]init] ;
[instanceC sendArray:[instanceA setArray:someArray] ]; //some array allocated in b
[instanceA release];
//in ClassC i have defined arrayC ,that gets array as a pointer from classB
Is array1 stay valid in ClassC after i released instanceA ?
Does every change made to the array in ClassB is also made to the arrayC ?
Is this the right way to work ? my goal is to control over the arrayC in ClassC so every change i do in ClassB will apply to the one in C also, AND to not lose this relation because of some autorelease.
Doing this with property will be better ? how ?
thanks.
The question is a bit unclear, so I’m guessing somewhat at what you’re trying to understand.
You ask “Does every change made to the array in ClassB is also made to the arrayC ?”
The answer is that neither class B or class C have an array, rather they both have a reference to an array. Now if both class B and class C hold the same reference, then a change via that reference in class B is visible in class C via it’s reference – as they are both referring to the same array. Copying references does not copy the array, create a new array, or anything like that, it just copies the reference.
If that is still confusing think of an array reference as the address of a house. You can write the address of the house down on as many pieces of paper as you like and pass them to as many people as you like, and if each person reads the paper and goes to the address they all end up at the same house. If one of those people break a window in the house all the other people see the broken window – there is only one house.
HTH and I’m not answering the completely wrong question!