Q:

Copying an NSArray with mutable copies of the original elements

I am creating an array of dictionaries in a class. I want to return a copy of that array to any other object that asks for it. This copy that is passed to other objects needs to be modified without modifying the original.
So I am using the following in a getter method of my class that holds the "master" array:
[[NSMutableArray alloc] initWithArray:masterArray copyItems:YES];

However, this seems to make all the dictionaries inside immutable. How can I avoid this?
I think I am missing something here. Any help will be much appreciated!

A:

Another approach you could take is to use the CFPropertyListCreateDeepCopy() function (in the CoreFoundation framework), passing in kCFPropertyListMutableContainers for the mutabilityOption argument.  The code would look like:
NSMutableArray* originalArray;
NSMutableArray* newArray;

newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)originalArray, kCFPropertyListMutableContainers);

This will not only create mutable copies of the dictionaries, but it would also make mutable copies of anything contained by those dictionaries recursively.  Do note though that this will only work if your array of dictionaries only contains objects that are valid property lists (array, number, date, data, string, and dictionary), so this may or may not be applicable in your particular situation.

A:

Copying an array will create a copy of the array object, with references to the original content objects (appropriately retained.)
Per the documentation, -initWithArray:copyItems: initializes the new array with copies of the items in the original array. This copy is created by sending the original content objects -copyWithZone:, which will create an immutable copy in the case of mutable objects. 
If you need different behavior (i.e. mutable copies of the content objects, or deep copies of the content objects) you'll have to write your own convenience function/method to do so.

A:

One way would be to abuse Key-Value Coding to send mutableCopy to each of the dictionaries and autorelease to each of the copies. But that's a dirty, dirty hack, so don't do that. Indeed, you probably shouldn't be doing this in the first place.
Generally, when I see the words “array of dictionaries”, I get the idea that you're using dictionaries as substitutes for model objects. Don't do that. Write your own model classes; everything becomes much easier when you have your own custom properties and behavior methods to work with. (Some things more than others: Implementing AppleScript support is virtually impossible without a proper model layer.)
And once you have real model objects, you can implement NSCopying in them and not need to worry about mutable versus immutable, since you probably won't have a mutability distinction in your real model classes anyway. (I don't know about others, but I've never made such a distinction in my model classes.) Then you can just use the existing NSArray initWithArray:copyItems: method.

