Woodstock
Woodstock

Reputation: 22926

NSMutableArray behaviour with ARC

With ARC enabled when adding an object (e.g. object X) to an NSMutableArray, if that object X is changed directly elsewhere in code, should the "copy" inside the NSMutableArray also be updated?

Is the NSMutableArray just referencing the pointer of the original object?

Upvotes: 0

Views: 207

Answers (2)

Tom van der Woerdt
Tom van der Woerdt

Reputation: 29975

Not when you add like this :

[marray addObject:[object2 copy]];

But when you add it like this (as a reference), it will get updated:

[marray addObject:object2];

Upvotes: 3

Abizern
Abizern

Reputation: 150605

If it's a copy then no. If you've added it as a reference then yes. It's just a pointer to an object so any changes to your object will apply to the object reference in the array.

Don't be confused by the NSMutable part of the array. The same thing happens with a normal NSArray: if you change the object, then the array will have a reference to the changed object. The mutable part only applies to the array, not the objects in the array.

Upvotes: 3

Related Questions