从initWithCoder调用另一个init时指定的初始化程序警告:

时间:2017-04-21 11:26:56

标签: ios objective-c designated-initializer

我正在处理继承的代码库并尝试解决以下警告:

  

指定初始值设定项只应在超级'

上调用指定的初始值设定项      

指定的初始化程序错过了超级'调用超类的指定初始化程序

代码是:

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [self initWithFrame:[CDCUtility getScreenBounds]]; //switching to super breaks
    if (self) {
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setArray:[NSMutableArray new]];
        [self setButtonArray:[NSMutableArray new]];
        _graphicEQ = [[CDCEffectsGraphicEQ alloc] initWithFrame:CGRectMake((1024 / 2) - (811 / 2), 60, 860, 255)];
        [self addSubview:_graphicEQ];
        [_graphicEQ setDelegate:self];
        [self addBypassButtonToView];
        [self addFlatButtonToView];
        [self addScrollView];
    }
    return self;
}

从我可以看出,开发人员覆盖超类initWithCoder:(这是UIView)以允许加载自定义UI,并使用此initWithFrame:参数传递用于创建视图的自定义参数。

我看到有人说要改变[self initWithFrame:]'在initWithCoder:[super initWithFrame:]确实解决了警告,但它也绕过调用此处所需的功能来正确加载视图。

它的工作正常;我只是想减少所有可能的警告,所以我想知道是否可以做出改变来解决这个问题?

1 个答案:

答案 0 :(得分:1)

试试这个:

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

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self designatedInitializer];
    }
    return self;
}

- (void) designatedInitializer {
    [self setArray:[NSMutableArray new]];
    [self setButtonArray:[NSMutableArray new]];
    _graphicEQ = [[CDCEffectsGraphicEQ alloc] initWithFrame:CGRectMake((1024 / 2) - (811 / 2), 60, 860, 255)];
    [self addSubview:_graphicEQ];
    [_graphicEQ setDelegate:self];
    [self addBypassButtonToView];
    [self addFlatButtonToView];
    [self addScrollView];
}