Curnelious
Curnelious

Reputation: 1

nsmutuable array as an argument

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.

Upvotes: 0

Views: 80

Answers (2)

user1805716
user1805716

Reputation: 11

Is array1 stay valid in ClassC after i released instanceA ? As array1 is not allocated in the class A and if in class A we are not sending release message to array1 the array1 won't be released and will be valid after releasing instance A.

Does every change made to the array in ClassB is also made to the arrayC ? As ClassB and ClassC are referring to the same array changes made to array are reflected in both the classes.

Upvotes: 1

CRD
CRD

Reputation: 53000

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!

Upvotes: 0

Related Questions