简体   繁体   中英

Instance variable used while 'self' is not set to the result of [(super or self) init…]

My Code :

- (instancetype)initWithSender:(UIView*)sender withSuperView:(UIView*)superView withItems:(NSMutableArray*)items
{
    self = [CustomDropdownView loadInstanceFromNibWithiPad:NO];
    if (self) {
        self.frame = CGRectZero;

        [self.layer setBorderColor:[UIColor lightGrayColor].CGColor];
        [self.layer setBorderWidth:1.0];

        CGRect positionInSuper = [superView convertRect:sender.frame fromView:sender.superview];
        float height = superView.frame.size.height - positionInSuper.origin.y;
        if (height > 200.0) {
//            height = 200.0;
        }

        [superView addSubview:self];
        self.translatesAutoresizingMaskIntoConstraints = NO;

        self.constLeading = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:positionInSuper.origin.x];
        self.constTop = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeTop multiplier:1.0 constant:positionInSuper.origin.y + sender.frame.size.height];

        self.constWidth = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:sender.frame.size.width];
        self.constHeight = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0];

        [superView addConstraints:@[self.constLeading, self.constTop]];
        [self addConstraints:@[self.constWidth, self.constHeight]];

        [self layoutIfNeeded];

        self.arrItems = [[NSMutableArray alloc]initWithArray:items];

        [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([DropDownTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([DropDownTableViewCell class])];

        [self.tableView reloadData];
    }
    return self;
}

The first line

    self = [CustomDropdownView loadInstanceFromNibWithiPad:NO];

is very problematic. You should only assign the result of another init method, usually [super init]. Change this to a class method.

By the way, that's what the error message says.

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