Re: KVC array proxy objects
Re: KVC array proxy objects
- Subject: Re: KVC array proxy objects
- From: Ashley Clark <email@hidden>
- Date: Tue, 5 May 2009 11:49:05 -0500
The documentation[1] about accessor search order shows that it will
prefer a method named -<key> over the corresponding -countOf<Key> and -
objectIn<Key>AtIndex: methods.
Specifically you should read the section about the search pattern for
valueForKey. The mutableArrayValueForKey proxy only details how array
modifications will be made while object access falls back to the
valueForKey search ordering.
If you've declared the songs array as a @property on the class then it
will use that first for its' data access.
[1] http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/SearchImplementation.html
On May 5, 2009, at 8:53 AM, Aaron Braunstein wrote:
I'm trying to understand how the proxy object which is returned
from mutableArrayValueForKey: works and I've hit a bit of a wall. I
get what the proxy is and why it exists. I have a test app which
allows me to do things to/with the collection it represents and it
all works fine. The problem is that when I try to implement some of
the methods mentioned in the developer docs under the "Key-Value
Coding Accessor Methods" section. There are some methods there
which if implemented in the hosting object (i.e. the original
recipient of the mutableArrayValueForKey: call) are to be called by
the proxy when the proxy is asked to do various things. The hitch
is that in my test app I can't get the -countOf<key> or -
objectIn<key>AtIndex methods to be invoked.
In reading through the docs, it seems that a number of methods need
to be implemented before any of these (dare I call them 'proxy
methods'?) are called. I implemented a whole slew of them -
including a number that shouldn't need to be - and ended up with
this set: (The test app is based on a Playlist->Songs->Song model
where "songs" is the NSMutableArray which lives in the Playlist
class in which I'm interested in getting a count of its member songs.)
- (unsigned int)countOfSongs;
- (Song *)objectInSongsAtIndex:(unsigned int)index;
- (NSArray *)songsAtIndexes:(NSIndexSet *)indexes;
- (void)getSongs:(Song **)buffer range:(NSRange)inRange;
- (void)insertObject:(Song *)newSong inSongsAtIndex:(unsigned int)idx;
- (void)removeObjectFromSongsAtIndex:(unsigned int)idx;
Even simple test code like this fails:
Playlist *myPlaylist = [[[Playlist alloc] init] autorelease];
id arrayProxy = [myPlaylist mutableArrayValueForKey:@"songs"];
[arrayProxy insertObject:[[[Song alloc] initWithName:@"test"
andLength:10] autorelease] atIndex:0];
unsigned int theCount = [arrayProxy count];
I've got all the properties defined, all the methods written, etc.
Yet when I call [arrayProxy count] my countOfSongs method in the
Playlist class isn't touched. The right answer is returned, but
it's apparently coming from the runtime going to the array directly
and getting the answer via NSArray's count method.
Oddly enough, when I do the insertObject call in line #3 the
insertObject:inSongsAtIndex: method IS called... so some of this
stuff works as I believe it is supposed to. Unfortunately it's the
other stuff that's driving me nuts. I've been working on this one
for a couple of days now and have tried everything I could come up
with - including some really silly, paranoid stuff - and have made
no progress.
Can anybody help me with a suggestion as to what I might be doing
wrong or what I'm missing?
Thanks!
_______________________________________________
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