0

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?

4

2 回答 2

3

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.

于 2013-06-22T11:38:12.950 回答
3

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];
于 2013-06-22T11:39:44.233 回答