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: Ben Trumbull <email@hidden>
- Date: Sun, 6 Jul 2008 17:02:19 -0700
Steve,
First, have you considered the low tech approach of using a gcc
variadic macro (#define) ? <http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html
> You can generate a lot of template code this way. With one line
of code per key you can generate all the accessors you need.
A second option would be to override -mutableArrayValueForKey: to
return a proxy object that knows both the source object and the key
and fulfills the mutable KVC contract. Basically, this is creating a
subclass of NSMutableArray that knows how to properly handle your
source object, it's key.
On 10.5, you have an additional hook that is invoked before forwarding:
+ (BOOL)resolveInstanceMethod:(SEL)sel;
This is much more efficient than forwarding, and the result is cached,
so method resolution is only invoked once per selector. It does
require some nasty "parsing" of the selector, and use of the raw
objective-c runtime functions to register the new method on the class.
-forwardInvocation: is the least efficient choice, although it can be
handy under some circumstances where flexibility is the most important
design issue.
- Ben
_______________________________________________
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