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

标签 ios objective-c designated-initializer

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

Designated initializer should only invoke a designated initializer on 'super'

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

代码是:

- (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;
}

因此据我所知,开发人员覆盖了父类(super class) initWithCoder:(这是一个 UIView)以允许加载自定义 UI,并传入此initWithFrame: 使用自定义参数创建 View 的参数。

我看到有人说将 initWithCoder: 中的 [self initWithFrame:]' 更改为 [super initWithFrame:] 这确实解决了警告,但是它也绕过了调用此处正确加载 View 所需的功能。

它按原样运行良好;我只是想减少所有可能的警告,所以我想知道是否可以通过更改来解决这个问题?

最佳答案

试试这个:

- (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];
}

关于ios - 从 initWithCoder : 调用另一个 init 时指定的初始化程序警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43541555/

相关文章:

ios - Swipeview 滞后于项目更改

objective-c - 如何在 Objective-C 中打印单个数组元素?

iphone - NSArray 为空,filteredArrayUsingPredicate ios

objective-c - 在 Objective-C 中,指定初始化器总是被调用的规则并不总是被遵守?

ios - 如何从 iOS 的另一个 nsarray 中不存在的核心数据中删除对象?

ios - 删除 UITableView 中页眉/页脚/单元格之间的分隔

ios - UIScrollView 内的 MKMapView

ios - 如何在 UITableviewcell 中隐藏或显示 uibutton

ios - 调用父类(super class)指定的初始化程序调用子类

ios - 指定初始化程序?