• 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
Defining subclasses at run time
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Defining subclasses at run time


  • Subject: Defining subclasses at run time
  • From: Oleg Krupnov <email@hidden>
  • Date: Mon, 11 Jun 2012 12:35:13 +0300

Hi,

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).

How do I solve this problem?

I have an idea that I could spawn new subclasses of MyClass at run
time, for each new array of MyItems, assigning each class with a
different class-level context variable, and then instantiating items
from that subclass. In this way, +[_runtime_subclass_of_MyItem
getContext] would return a different context value for each array.
This would reuse the "isa" pointer that's already in any NSObject
instance.

Is this possible in Obj-C? Or is there a better idea?

Thanks!
_______________________________________________

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: Defining subclasses at run time
      • From: Greg Parker <email@hidden>
  • Prev by Date: Re: Why does NSDocument not care about saving Viewer-role document types
  • Next by Date: setting preselected file for nsopenpanel
  • Previous by thread: Re: NSDocument -canAsynchronouslyWriteToURL::: never called
  • Next by thread: Re: Defining subclasses at run time
  • Index(es):
    • Date
    • Thread