Re: Defining subclasses at run time
Re: Defining subclasses at run time
- Subject: Re: Defining subclasses at run time
- From: Rainer Brockerhoff <email@hidden>
- Date: Mon, 11 Jun 2012 13:50:00 -0300
On Jun 11, 2012, at 13:20 , email@hidden wrote:
> Date: Mon, 11 Jun 2012 12:35:13 +0300
> From: Oleg Krupnov <email@hidden>
> Message-ID:
> <email@hidden>
>
> I'm solving the following problem. I have a huge array of items of the
> same class MyItem. All items need to share a certain global "context"
> variable. Normally, I would wrap access to this variable in a
> class-level (+) method of MyItem, like +[MyItem getContext].
>
> Now the problem is that I need multiple arrays of MyItems have
> different contexts. The solution could be to add the context pointer
> as an ivar to each MyItem instance, but I would not like to waste
> memory on that (the arrays are huge).
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.
However, what I used in a similar situation is objc_getAssociatedObject() and objc_setAssociatedObject() in objc/runtime.h. Look them up.
HTH,
--
Rainer Brockerhoff <email@hidden>
Belo Horizonte, Brazil
"In the affairs of others even fools are wise
In their own business even sages err."
Weblog: http://www.brockerhoff.net/blog
_______________________________________________
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