Re: mutableArrayValueForKey: truth or dare
Re: mutableArrayValueForKey: truth or dare
- Subject: Re: mutableArrayValueForKey: truth or dare
- From: Roland King <email@hidden>
- Date: Fri, 18 Jun 2010 08:56:37 +0800
On 18-Jun-2010, at 6:06 AM, Ken Thomases wrote:
>
>>> - in particular, that if you don't
>>> also implement -countOfItems, -objectInItemsAtIndex:,
>>> -insertObject:inItemsAtIndex:, and -removeObjectFromItemsAtIndex:,
>>> then every change in the mutable array through the proxy replaces the whole
>>> array. Is this true? Thx - m.
>>
>> This is true. How else could the proxy do its work? The docs are pretty helpful on how the proxy decides what to do.
>
> And the docs also point out the inefficiencies. From <http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/KeyValueCoding/Concepts/SearchImplementation.html#//apple_ref/doc/uid/20000955-SW2>:
>
>> Note: The repetitive set<Key>: messages implied by the description in step 2 are a potential performance problem. For better performance, implement methods that fulfill the requirements for Step 1 in your key-value coding-compliant class.
>
One point to note however, if the receiver implements -set<key> for the array variable then yes it replaces the whole array every time, if however it doesn't and you just have a class variable called <key> or _<key> then the whole array is not replaced, the method you call on NSMutableArrayValueForKey: is passed through to that underlying variable which does what you might expect and operates on the array variable directly.
That's not such a rare case. Often something exposed as a readonly array property is backed by a mutable array variable of the same name in which case it 'just works'. Where you may get into difficulty relying on this is if you, even internally in the class, on day write yourself a set<key> method, then all your indexed mutations are going to start replacing the whole array again.
It is best to implement the methods even if that is often an exercise in typing skills. _______________________________________________
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