Re: How to create to-many accessor methods at runtime
Re: How to create to-many accessor methods at runtime
- Subject: Re: How to create to-many accessor methods at runtime
- From: Steve Weller <email@hidden>
- Date: Tue, 08 Jul 2008 20:36:51 -0700
On Jul 8, 2008, at 8:55 AM, Mike Abdullah wrote:
On 8 Jul 2008, at 15:45, Steve Weller wrote:
On Jul 8, 2008, at 2:28 AM, Mike Abdullah wrote:
On 8 Jul 2008, at 07:10, Steve Weller wrote:
What I am attempting to do is not working. -valueForKey: does
not look for methods with -respondsToSelector: to figure out if
methods exist sufficient to have KVC. So overriding this has no
effect. My only option, it seems, is to use
+resolveInstanceMethod and actually create the methods that are
needed.
Or, you know, just override -valueForKey: ? NSArray and the other
contain classes do just that to get their custom behaviour.
Then I'd need to create an NSArray proxy, probably by subclassing
NSArray. This was the way I first thought of and decided not to go
that route.
My current thinking is to hard-code -count and -objectForKey (and
possibly others) in a helper object, then vend instances of that
according to what the array should implement. Each of those objects
ultimately gets its data from a single source, so if I change
values, all the arrays' customers see the changes. It is likely
that I will want KVO compliance one day too and that will not work
with the helpers.
Well I am now officially confused. As I understood it, you were
writing a class that could have any number of keys, each one of
which was a one-to-many relationship. Since the number of keys was
undefined, it wasn't possible to write accessor methods for them,
and so instead, you were overriding -respondsToSelector: and friends
to fool the KVC system into thinking that you had written the
appropriate accessor methods.
But, the implementation of -valueForKey: specifically does not use -
respondsToSelector:, and so you can't the fool the system that way.
And so, I'm suggesting simply overriding -valueForKey: in your
custom class in order for it to return a suitable array.
That's my problem. What makes the array "suitable"?
However, from your last mail, it seems I have the wrong end of the
stick, as you think it requires overriding -valueForKey: in a custom
NSArray subclass. So, um, any chance of some clarification?
If I want to override -valueForKey: and return an array object (as I
am supposed to since this is what is expected), then whatever object I
return has to behave as an NSArray does. So unless I want to duplicate
the functionality of NSArray, I'll have to subclass it.
That's my thinking anyway.
However I now have another idea. I can have a central object that owns
the data, say called Machine. Other objects can request object
instances, say Shape, from Machine that implement different operations
on that data and reference them by a key. Shape objects themselves can
implement the to-many accessors, and so can respond to objectForKey
for the property I want.
Load the machine up with recipes:
[machine -addRecipe:recipeA forKey:@"circle" withParameter:345];
[machine -addRecipe:recipeB forKey:@"square" withParameter:778];
Ask machine for objects that implement the recipe on the data. These
all use the same data stored in the machine instance but process it in
different ways.
shape1 = [machine shapeForKey:@"circle"];
shape2 = [machine shapeForKey:@"square"];
Get arrays of the things I am actually interested in. If the data in
machine changes, the array data changes. "points" and "lines" have
hard-coded to-many accessor methods.
arrayOfPoints = [shape1 valueForKey:@"points"];
arrayOfLines = [shape1 valueForKey:@"lines"];
_______________________________________________
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