Re: NSDictionary allValues not mutable
Re: NSDictionary allValues not mutable
- Subject: Re: NSDictionary allValues not mutable
- From: Ken Thomases <email@hidden>
- Date: Sat, 9 Oct 2010 15:17:27 -0500
On Oct 9, 2010, at 3:06 PM, Trygve Inda wrote:
>> I have an NSMutableDictionary made from a plist with
>> CFPropertyListCreateDeepCopy so that all leaves and containers are mutable.
>>
>> In this plist is an dict whose values I want to move into a mutable array to
>> be displayed and edited in an NSTable.
>>
>> [myDict allValues] gets the correct array and works in the table, but it is
>> made immutable. Is there any better way to keep the mutability other than
>> using allKeys then stepping through each one, obtaining its object and
>> adding it to an NSMutableArray that I made with alloc/init?
>>
>> Why doesn't allValues simply preserve the mutability? I would have guessed
>> that internally it did the above steps.
Why would the mutability of the dictionary change the mutability of the returned array? Are you expecting that mutating the array would mutate the dictionary?!? That really couldn't work. What would it mean to add an object to the array?
If that's not what you're expecting, then there's no reason for the returned array to be mutable. Certainly, the method declaration tells you it returns an immutable array. So, as noted in the general Cocoa documentation, you _must not_ interrogate the object in the hopes that it _might_ be mutable and then mutate it. It is (remotely) conceivable that such a method would return part of the mutable dictionary's internal state, which might in fact be mutable, but the fact that you're given only a pointer to its immutable superclass means you're not entitled to mutate it.
> PS: I do not want to use mutableCopy because I want to objects in the array
> and the objects in the dictionary to refer to the same objects... Eg I do
> not want two of each object.
Yes, you do want to use mutableCopy because it doesn't do what you think it does. It gives you a new array containing the same objects as in the immutable array and which are the values in the dictionary. The copy is shallow, not deep.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden