Jimmery
Jimmery

Reputation: 10139

Copying a dictionary changes NSMutableArray into NSArray?

So I have this NSMutableDictionary object:

pdata=[[NSMutableDictionary alloc] initWithObjectsAndKeys:
       @"",@"pid",
       @"",@"pname",
       [[NSMutableArray alloc] initWithCapacity:1],@"ilist",
       nil];

And then I copy this object into another object like this:

NSMutableDictionary *pdataCopy=[[NSMutableDictionary alloc] initWithDictionary:pdata copyItems:TRUE];

But once Ive done this, pdataCopy.ilist is now an NSArray instead of NSMutableArray.

How can I copy a dictionary object whilst maintaining the mutability of the properies inside it?

Upvotes: 0

Views: 531

Answers (2)

Hot Licks
Hot Licks

Reputation: 47739

You have three options:

  1. Don't specify copyItems:YES
  2. Scan through the dictionary after copying and replace NSArrays with NSMutableArrays (using mutableCopy) as desired.
  3. Create your own subclass of NSMutableArray that responds to copyWithZone by producing a mutable copy of itself (and use objects of that class in your dictionary).

Upvotes: 0

sunkehappy
sunkehappy

Reputation: 9101

Actually you can't. You can get a mutable array by

NSMutableArray *mutableArray = [pdataCopy.ilist mutableCopy]

Upvotes: 2

Related Questions