Re: Defining subclasses at run time
Re: Defining subclasses at run time
- Subject: Re: Defining subclasses at run time
- From: Jens Alfke <email@hidden>
- Date: Mon, 11 Jun 2012 11:43:43 -0700
On Jun 11, 2012, at 9:50 AM, Rainer Brockerhoff wrote:
> You could subclass NSArray (tricky because it's a class cluster), adding the context variable as an ivar to that; or make your own class containing that context and the actual array, and implementing all the proper methods.
I don't think that will help unless there's a pointer from MyItem to its containing array.
> However, what I used in a similar situation is objc_getAssociatedObject() and objc_setAssociatedObject() in objc/runtime.h. Look them up.
I'll bet that will add more overhead than the size of a pointer, to each object, so you might as well add the pointer to MyItem.
My take on this is that creating subclasses on the fly is possible (using the low-level ObjC API) but it seems kind of an overly complex hack, i.e. the kind of thing Ruby fanatics would do ;-)
Is adding 4 or 8 bytes to each object really a big deal?
Is there a way you can make the other instance data smaller to compensate for adding a pointer?
Alternatively, if there are less than 256 'types' of MyItem, you could add a byte field to the class (possibly sneaking it into space that's already there by making use of struct packing) and use that as an index into a global table of 256 kinds.
—Jens
_______________________________________________
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