简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM