Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Designated initializer missing a super call to designated initializer of the super class

my code is here:

(instancetype)initWithFrame:(CGRect)frame
{
self = [[[NSBundle mainBundle] loadNibNamed:@"LSCouponADView" owner:Nil options:nil] objectAtIndex:0];
if (self) {

}

    return self;
}

Then xcode has warning

Designated initializer missing a super call to designated initializer of the super class

when I building it.

like image 671
waterKnight Avatar asked May 25 '16 10:05

waterKnight


2 Answers

You need to add this line in this method.

self = [super initWithFrame:frame];
if(self) {

}
return self;

From Apple Doc.

The Designated Initializer

The initializer of a class that takes the full complement of initialization parameters is usually the designated initializer. The designated initializer of a subclass must invoke the designated initializer of its superclass by sending a message to super. The convenience (or secondary) initializers—which can include init—do not call super. Instead they call (through a message to self) the initializer in the series with the next most parameters, supplying a default value for the parameter not passed into it. The final initializer in this series is the designated initializer.

like image 166
Mahendra Avatar answered Nov 08 '22 15:11

Mahendra


you should make your class some thing like this:

Run tIme init:>

-(instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
        if(!self){
            return nil;
        }

        NSBundle *mainBundle = [NSBundle mainBundle];
        NSArray *views = [mainBundle loadNibNamed:@"LSCouponADView" 
                                            owner:nil 
                                          options:nil];
//above nib name should not be hard coded, it should be like this:
//NSArray *views = [mainBundle loadNibNamed:NSStringFromClass([self class]) 
                                                owner:nil 
                                              options:nil];
        [self addSubview:views[0]];

    return self;
    }

You should also override for xib initialization:

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

        NSBundle *mainBundle = [NSBundle mainBundle];
        NSArray *views = [mainBundle loadNibNamed:@"LSCouponADView" 
                                                owner:nil 
                                              options:nil];
        [self addSubview:views[0]];

    return self;
    }

over all you can make a common method for loading from nib.

like image 45
maddy Avatar answered Nov 08 '22 14:11

maddy