GoldenJoe
GoldenJoe

Reputation: 8012

How to properly initialize subclasses in Objective C?

I have two UIViews. They both require custom initialization, and it seems repetitive and bug-prone to simply copy initialization code in both initWithFrame: and initWithCoder:. However, having a standard initialization method name like initMyStuff results in the child's version being called twice. I could resolve this by giving each class a unique initialization name like initStuffForCustomViewParent, but I would prefer if there was a way to maintain a standard custom initialization method name in order to make my code more readable. Here is a sample implementation of the problem:

@implementation CustomViewParent
-(id)initWithFrame:(CGRect)aRect
{
    if(self=[super initWithFrame:aRect])
        [self initMyStuff];
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super initWithCoder:aDecoder])
        [self initMyStuff];
    return self;
}

-(void)initMyStuff
{
    // Do stuff (never called)
}
@end

@implementation CustomViewChild
-(id)initWithFrame:(CGRect)aRect
{
    if(self=[super initWithFrame:aRect])
        [self initMyStuff];
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super initWithCoder:aDecoder])
        [self initMyStuff];
    return self;
}

-(void)initMyStuff
{
    // Do stuff (called twice)
}
@end

// SAMPLE
// Calls CustomViewChild's initMyStuff twice and CustomViewParent's initMyStuff never
CustomViewChild *obj = [[CustomViewChild alloc] init];

Upvotes: 2

Views: 338

Answers (1)

Rob Napier
Rob Napier

Reputation: 299575

CustomViewChild shouldn't call initMyStuff. Just call this in the parent and the child's initMyStuff should call [super initMyStuff].

But in either case, don't call it initMyStuff because that suggests it's an init method, which implies certain behaviors. Call it something like setup (assuming the goal is to have it called from both initWithFrame and initWithCoder:).

Upvotes: 7

Related Questions