views:

35

answers:

2

If I have a NSMutableArray:

NSMutableArray *principalTable;

and I have a other NSMutableArray:

NSMutableArray *secondTable;

and I do this:

[secondTable addObject:@"string"];

[principalTable addObject: secondTable];

[secondTable removeAllObjects];

The principalTable has 1 object, but this object has nothing inside. So my question is:

  • When I add a object in a array, the array point on the object, or copy the object in the array?
  • And is it the same thing when I add in a nsmutabledictionnary?
+2  A: 
[secondTable addObject:@"string"];

[principalTable addObject: secondTable];

[secondTable removeAllObjects];

When you do the above, principalTable will contain a reference to the secondTable NSMutableArray instance and will not directly contain the string. Thus, when you removeAllObjects, you are emptying secondTable and principalTable will continue to hold a reference to that now-empty mutable array.

None of your code copies anything.

NSMutableDictionary copies keys. Not values.

bbum
So if I'm in a loop I must create a new table each time and add it to principalTable?
alex
Depends on your needs; if you want an array of arrays then you would need a new array each pass. If you want an array of strings, you don't need the second array at all.
bbum
A: 

The array contains a reference (i.e. a pointer) to the object that you added. Thus when you removed all objects from secondTable, you removed the from the same array that is an element of principle table.

Jason Jenkins