• 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: Philip Dow <email@hidden>
  • Date: Tue, 25 Apr 2006 12:44:00 +0200

Hi Sherm, thanks for the reply. After checking out the documentation on class clusters, that's basically the idea, except my subclases aren't all private. With an implementation like this I suppose I could make them private though.

I am having to do it how you suggested, releasing self and then returning an instance of the appropriate class allocated and initialized. Just calling the designated initializer on the class didn't work because I didn't have it implemented as "class factory" method, allocation and initialization in one method.

What a neat way to create an object though. I can now pass in a filename to EntryObject as I initialize it and the instance will determine from the file's UTI definition which subclass it should be, release itself, and allocate a separate instance of the appropriate subclass, populating it with the data associated with that subclass. Cool.

-Phil

On Apr 24, 2006, at 11:05 PM, Sherm Pendley wrote:

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
References: 
 >Creating an instance of a subclass from a superclass initilization (From: Philip Dow <email@hidden>)
 >Re: Creating an instance of a subclass from a superclass initilization (From: Sherm Pendley <email@hidden>)

  • Prev by Date: Re: Superimpose a NSTextField over NSImageView? [SOLVED]
  • Next by Date: getting raw data from image.
  • Previous by thread: Re: Creating an instance of a subclass from a superclass initilization
  • Next by thread: Re: Getting a table to resize columns. I'm doing everything right but it's not working?
  • Index(es):
    • Date
    • Thread