On Jun 11, 2012, at 13:20 , cocoa-dev-requ...@lists.apple.com wrote:
> Date: Mon, 11 Jun 2012 12:35:13 +0300
> From: Oleg Krupnov <oleg.krup...@gmail.com>
> Message-ID:
>       <cak83q-b44u6dh5hx7gdjj676_vot_6c-+dngxsabgihpjh3...@mail.gmail.com>
> 
> 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  <rai...@brockerhoff.net>
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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to