Replies

There's not much reason to do the second one since NSMutableArray does everything that NSArray does, unless you expect the mutable array to change and you want a copy of its current state.

Two things to keep in mind: (1) both of these methods return an object with a retainCount of 1, just like alloc+init does. You own these objects and must release them somewhere. And (2) neither method makes copies of the items themselves, just the pointers. It's like having a list of postal adresses and photocopying the list - that doesn't automagically create duplicate houses. That would be a "deep copy."

There's not much reason to do the second one since NSMutableArray does everything that NSArray does, unless you expect the mutable array to change and you want a copy of its current state.

Two things to keep in mind: (1) both of these methods return an object with a retainCount of 1, just like alloc+init does. You own these objects and must release them somewhere. And (2) neither method makes copies of the items themselves, just the pointers. It's like having a list of postal adresses and photocopying the list - that doesn't automagically create duplicate houses. That would be a "deep copy."

Smasher,

I didn't realize that sending a copy method to a mutable array (e.g. [aMutableArray copy]) would return an immutable copy, but I just looked into it, and you are absolutely right.

That seems counter-intuitive.

Regards,
Duncan C
WareTo

Animated GIF created with Face Dancer, available for free in the app store.

I didn't realize that sending a copy method to a mutable array (e.g. [aMutableArray copy]) would return an immutable copy, but I just looked into it, and you are absolutely right.

That's an oddity in Apple's docs that I've never seen explained - whenever a class lists a protocol but has another class in parenthesis, it apparently means that the class only implements the protocol through the superclass. For example, this:

There's important information there - like if you call NSCoding to encode a mutable array, you get an immutable array back when you decode - but only if you know the secret code. Otherwise it's a nasty surprise.

It is, but if it weren't true then "copy" and "mutableCopy" would do the same thing on a mutable array, and you'd need a "immutableCopy" method to get a nonmutable copy; so I can see why they done what they done.