designated initializer conventions
designated initializer conventions
- Subject: designated initializer conventions
- From: Mike Bolton <email@hidden>
- Date: Sun, 30 Jan 2005 05:29:57 -0500
While searching for ways to draw a border around an NSView during a
drag and drop operation, I came across the following message:
http://www.cocoabuilder.com/archive/message/2004/5/5/106346
I was hoping someone could shed some light on exactly how to conform
to the conventions of "designated initializers" in such a case. In
the code from the message, there's only one init method:
- (id) initWithParent: (NSView *) inParent
{
NSRect parentFrame = [inParent frame];
if (self = [self initWithFrame: parentFrame]) {
//do something
}
return self;
}
now, from what I've read in the Objective-C Programming Language
manual (and from
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSObject.html#//apple_ref/doc/uid/20000050/init),
it states that "the subclass is obliged to cover (override) only the
designated initializer of the superclass". I've also read that "The
designated initializer in a class must, through a message to super,
invoke the designated initializer in a super class".
What confuses me is that the above initializer sends the initWithFrame
to self, rather than super.. That would lead me to believe that
initWithFrame is the designated initializer for the class (unless this
is a typo and the initWithFrame should've been sent to super). In
order to conform with the rules mentioned in the previous paragraph,
shouldn't the class implement initWithFrame, which would simply call
"if (self = [super initWithFrame...]", or is this being overly
pedantic, and unnecessary, given that the designated initializer of
the subclass is the same as the superclass, in which case you don't
need to the subclass to override it.
Or should initWithParent actually be considered the designated
initializer, since it allows more flexibility in the creation of the
object, and the subclass should thus implement something similar to
the following:
- (id) initWithFrame:(NSRect)aFrame
{ return [self initWithParent:nil]; }
- (id) initWithParent: (NSView *) inParent
{
NSRect parentFrame;
if (inParent == nil)
parentFrame = NSMakeRect (0,0,0,0);
else
parentFrame = [inParent frame];
if (self = [super initWithFrame: parentFrame]) {
//do something
}
return self;
}
I'm in no way criticizing the code posted in the original message, I'm
just trying to understand exactly how the rules of these designated
initializers should be followed. If anyone has any input, I'd
appreciate it. Thanks,
Mike
_______________________________________________
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