子类应该在直接超类中调用指定的初始值设定项吗?

我已经看到一些示例代码,让我想知道在超类中调用指定的初始化程序。 说我有一些代码:

@interface NewTableViewCell : UITableViewCell { } @end @implementation NewTableViewCell - (id) initWithFrame: (CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Do some stuff } return self; } @end 

请注意, initWithFrameUIView的指定初始化程序,而不是UITableView 。 这段代码应该始终调用[UITableViewCell initWithStyle:reuseIdentifier:] ,还是取决于编码器的意图?

子类化时,指南是指定的初始化程序必须调用其超类的指定初始化程序。

另一个指导原则是子类需要覆盖超类的指定初始化程序以调用新的指定初始化程序。

如果UITableViewCell遵循这个指南(并且确实如此;我在类别的帮助下进行了测试),它会覆盖其超类的指定初始化程序( UIViewinitWithFrame:以调用新的指定初始化程序( initWithStyle:reuseIdentifier: 。 因此,如果你在UITableViewCell调用initWithStyle:reuseIdentifier:它将调用initWithStyle:reuseIdentifier: ,它将调用initWithFrame: on superUIView )。

因此,它需要一个额外的方法调用,但最终将通过initWithStyle:reuseIdentifier: .

同样,最佳实践是指定的初始化程序必须调用超类的指定初始化程序,而不是指定初始化程序的任何其他初始化程序必须调用指定的初始化程序。 来自“指定初始化器” :

一般原则 :类中的指定初始值设定项必须通过超级消息调用超类中的指定初始值设定项。

指定的初始化器通过消息链接到super,而其他初始化方法通过消息链接到指定的初始化器。

我同意它取决于编码器的尝试,但编码器应该总是尝试使用指定的初始化器。 考虑一下您可能已经编写的初始化程序,它们可能会为您的对象做一些额外的工作,使其处于可用或期望的状态。 如果您正在覆盖初始化程序,就像您在示例中所做的那样,您也应该调用重写的初始值设定项。 如果那是一个自定义的init方法,那么你会想要调用指定的初始化程序,因为对于UITableViewCell ,这是公开设置reuseIdentifier的唯一方法。

 //Override initWithFrame //Fine although it may not (should not) get called for a UITableViewCell - (id) initWithFrame: (CGRect)frame { self = [super initWithFrame:frame]; //Design a custom initializer to gather parameters for supers default initializer -(id)initWithCustomObject:(id)object style:(UI..Style)style reuseIdentifier:(NSString*)rid { //This should call initWithStyle:reuseIdentifier: