• 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: Creating an instance of a subclass from a superclass initilization
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Creating an instance of a subclass from a superclass initilization


  • Subject: Re: Creating an instance of a subclass from a superclass initilization
  • From: Sherm Pendley <email@hidden>
  • Date: Mon, 24 Apr 2006 17:05:18 -0400

On Apr 24, 2006, at 3:45 PM, Philip Dow wrote:

I have an object called EntryObject. There are two subclasses EntryObjectFoo and EntryObjectBar. There are times when I am allocating and initializing an EntryObject and it determines from the objects I pass into this particular initializer that it should be a subclass of itself. Instead of calling self = [super init], could I call self = [EntryObjectFoo designatedInitializer] so that the subclass receives a redirected init and returns the appropriate class?

In general principle yes - if I understand you correctly, then what you're doing is called a "class cluster" and is well supported in Cocoa.


In one particular though, I'd implement it differently. Even at this low level, standard Cocoa memory management rules apply. Since the "self" that's passed to your initializer was created with +alloc, it needs to be released. Then you can alloc/init a new object to return. Like this:

	[self release];
	return [[EntryObjectFoo alloc] designatedInit ...];

sherm--

Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Creating an instance of a subclass from a superclass initilization
      • From: Philip Dow <email@hidden>
References: 
 >Creating an instance of a subclass from a superclass initilization (From: Philip Dow <email@hidden>)

  • Prev by Date: can't archive NSValue
  • Next by Date: NSComboBox in a draggable view
  • Previous by thread: Creating an instance of a subclass from a superclass initilization
  • Next by thread: Re: Creating an instance of a subclass from a superclass initilization
  • Index(es):
    • Date
    • Thread