Re: noob questions regarding KVC accessors for a mutable array property
Re: noob questions regarding KVC accessors for a mutable array property
- Subject: Re: noob questions regarding KVC accessors for a mutable array property
- From: "Clark Cox" <email@hidden>
- Date: Sun, 29 Jun 2008 12:20:53 -0700
On Sun, Jun 29, 2008 at 8:33 AM, Stuart Malin <email@hidden> wrote:
> I do have the indexed accessors implemented. I also have an accessor that
> returns an array of the objects -- not sure why I did that... force of habit
> from other accessors (having a setter -set<Key> and a getter -<Key>). I
> certainly can understand one reason why this wouldn't be good, because
> should some code somewhere acquire this immutable array, and hang on to it,
> then that list of objects could easily become out of sync.
>
> That said, I "instrumented" my accessor and the index accessors to see what
> is being used. Oddly (at least to me), if I do have the
> array-returning-accessor implemented, it is actually invoked by Cocoa code:
> -[NSBinder _valueForKeyPath:ofObject:mode:raisesForNotApplicableKeys:]
> Perhaps because the mutable array property is the content model for an
> NSArrayController, which has its arrangedObjects bound to a Table View.
>
> I'm curious why, if it is advised to not have the -<Key> accessor, that
> Cocoa itself invokes the -<Key> accessor preferentially to the index
> accessors?
There's nothing wrong with having the -<Key> accessor. The problem
comes from having the -set<Key>: accessor *without* also implementing
the indexed setters; in such a case *every* single modification to the
proxy array will end up calling set<Key>: which can get horribly
inefficient.
>
>
> On Jun 26, 2008, at 9:49 PM, Scott Anguish wrote:
>
>> the better way is to implement the indexed accessors described in the KVC
>> doc.
>>
>> in fact I know at least one engineer would would like the doc to
>> specifically say that you should NOT have an accessor that returns an array
>> like this.
>>
>>
>> On Jun 26, 2008, at 7:54 PM, Stuart Malin wrote:
>>
>>> Separately, I have an accessor -attendees: of the Party class, which is
>>> currently implemented as:
>>>
>>> - (NSArray*) attendees
>>> {
>>> return [NSArray arrayWithArray:attendees]; // "attendees" is
>>> an NSMutableArray, and is an ivar
>>> }
>>>
>>> I intentionally do not return the underlying mutable array, because I
>>> don't want other code accessing the content without going through the
>>> accessors.
>>>
>>> Is my implementation reasonable? Or are there preferable ways to do this
>>> (such as to return a copy of the mutable array)?
>
> _______________________________________________
>
> 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
>
--
Clark S. Cox III
email@hidden
_______________________________________________
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