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

Re: Defining subclasses at run time


  • Subject: Re: Defining subclasses at run time
  • From: Greg Parker <email@hidden>
  • Date: Mon, 11 Jun 2012 11:59:56 -0700

On Jun 11, 2012, at 2:35 AM, Oleg Krupnov <email@hidden> wrote:
> 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).

First, verify that adding the ivar would in fact cost memory. Objective-C objects are allocated by malloc(). For most sizes, malloc() allocates additional bytes rather than returning exactly the number of bytes you asked for. It's possible that adding the ivar will not push you into the next malloc() size bucket, in which case the extra ivar is free.

To check this, you can call malloc_size() from malloc/malloc.h to see the actual allocated size of your object. Do it with and without the new ivar and compare the results. If they are the same then just add the context ivar.


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

This can work if you aren't afraid of the low-level runtime machinery. Note that Key-Value Observing already uses the trick of changing the isa pointer, so you won't be able to use KVO with instances of MyItem.

Look up objc_allocateClassPair() and objc_registerClassPair() to create the subclass objects. You can ask for extra storage on the class objects, accessible via object_getIndexedIvars(), to store your context pointer.


--
Greg Parker     email@hidden     Runtime Wrangler



_______________________________________________

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

References: 
 >Defining subclasses at run time (From: Oleg Krupnov <email@hidden>)

  • Prev by Date: Re: Defining subclasses at run time
  • Next by Date: Re: setting preselected file for nsopenpanel
  • Previous by thread: Defining subclasses at run time
  • Next by thread: Re: Defining subclasses at run time
  • Index(es):
    • Date
    • Thread