• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
re: How to create to-many accessor methods at runtime
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: How to create to-many accessor methods at runtime
      • From: Steve Weller <email@hidden>
  • Prev by Date: Package data store for app with agents
  • Next by Date: Re: plist parsing in c
  • Previous by thread: Re: How to create to-many accessor methods at runtime
  • Next by thread: Re: How to create to-many accessor methods at runtime
  • Index(es):
    • Date
    • Thread