0

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.

6
  • 1
    What does sendArray: do? What is arrayC? How is it set? Commented Nov 7, 2012 at 8:02
  • sendArray change the values of the vector , and arrayC - i wrote in the question what he is-he defined in class c as an nsmutuablearray. Commented Nov 7, 2012 at 8:06
  • Advanced Memory Management Programming Guide Commented Nov 7, 2012 at 8:08
  • As you ask related to "autorelease" I assume that you do not use ARC? If so then we need so see more of your code. Where/when and how are the related arrays instanciated, retained, released or autoreleased? Without the exact code your questions cannot be answered. Commented Nov 7, 2012 at 8:47
  • 1
    BTW, a setter method of a property abc should be named setAbc. You should not use the prefix "set" for any method unless it is a setter. This is not directly related to your question but following the cocoa naming conventions does help avoiding issues around the life cycle of objects and around memory management. Believe me. Once I understood the principles and followed the rules (the basic rules at least) things improved significantly in my projects. Escpecially using codes from other developers or tutorials is more seamless. Commented Nov 7, 2012 at 8:51

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.