My classes often end up looking something like this with the init code factored out into a separate method called from any of the initializers.
-(void)My_Class_Internal_Init
{
.. init code ..
}
-(id)init
{
self = [ super init ];
if( self )
[ self My_Class_Internal_Init ];
return self;
}
-(id)initWithCoder:(NSCoder*)coder
{
self = [ super initWithCoder:coder ];
if( self )
[ self My_Class_Internal_Init ];
return self;
}
-(id)initWithFrame: … // etc etc
{
self = [ super init ];
if( self ){
NSLog(@"\n\n init"); }
return self;
}
-(id)initWithCoder:(NSCoder*)coder
{
self = [ super initWithCoder:coder ];
if( self )
NSLog(@"\n\n initWithCoder");
return self;
}
Only initWithCoder: is reached; NSLog outputs from there and a breakpoint stops there.
init and initWithFrame: are not reached. No NSLog outputs. No breakpoint stops there.
—This raises a new question: Why isn’t ’init’ reached at all?—
2) InitWithFrame: is unremarkable. For brevity, it contains:
_path = [[NSBezierPath alloc] init];
. . . generate random lines . . .
[_path closePath];
initialize a variable
I will gladly supply the whole thing if anyone wants to look at it.
3) Next I tried initWithCoder: by simple substitution, without knowing what I was doing.
-(id)initWithCoder:(NSCoder*)coder{
self = [ super initWithCoder:coder ];
//- (id)initWithFrame:(NSRect)frame {
// self = [super initWithFrame:frame];
It worked! But this is not the way I want to program. I need to know why initWithFrame stopped working, how to use initWithCoder: correctly, and what potential pitfalls are out there. There may be more than this one: