Re: Convenience initialisers
Re: Convenience initialisers
- Subject: Re: Convenience initialisers
- From: Ken Thomases <email@hidden>
- Date: Sun, 5 Jul 2009 12:59:38 -0500
On Jul 5, 2009, at 12:40 PM, DKJ wrote:
I'm trying to write one of those convenient class initialisers for
one of my objects. On my still-incomplete understanding of these
matters, this is what I need to have:
Header:
+ (MyObject *)myObject;
Implementation:
+ (MyObject *)myObject
{
return [[[self alloc] init] autorelease];
}
(I'd have the usual init method in here as well, of course.)
Have I done this correctly?
Yup.
I've been looking in the docs for an explicit example of this kind
of initialiser, but haven't found one yet.
I don't find one, either. Just so you're aware, such methods are also
called "convenience constructors" or "class factory methods". You
might file a bug on the documentation requesting an example
implementation.
On Jul 5, 2009, at 12:47 PM, Dimitri Bouniol wrote:
You can't tell 'self' to be allocated. Try this instead:
You can when "self" is a class object. In fact, you should message
self rather than the hard-coded class name in such a convenience
constructor. That way, if the method is invoked on a subclass it
still allocated the correct kind of object. (Consider, for example,
the +string... methods of NSString, as sent to NSMutableString.)
Regards,
Ken
_______________________________________________
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