alex
alex

Reputation: 879

Question about NSMutableArray

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:

Upvotes: 1

Views: 62

Answers (2)

Jason Jenkins
Jason Jenkins

Reputation: 5382

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.

Upvotes: 0

bbum
bbum

Reputation: 162722

[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.

Upvotes: 2

Related Questions