指定初始化程序?

Designated Initializer?

apples 文档中的说法是什么:

Sometimes the designated initializer of a superclass may be sufficient for the subclass, and so there is no need for the subclass to implement its own designated initializer. Other times, a class’s designated initializer may be an overridden version of its superclass's designated initializer. This is frequently the case when the subclass needs to supplement the work performed by the superclass’s designated initializer, even though the subclass does not add any instance variables of its own (or the instance variables it does add don’t require explicit initialization).

Apple's Documentation

是不是说我不必为 subclass 创建一个指定的初始值设定项,而 superclass 指定的初始值设定项就足够了,如果是这样,subclasses 属性被初始化?如果允许,在什么情况下?那将如何工作?

此外,如果您覆盖 DI,您如何从子class 调用该方法,因为父 class 具有与您相同的 DI?需要补作业是什么意思?

示例:

有一个超级class AinitWithName: DI.

现在创建子class B。如果你想要相同的 DI 并且你不需要任何额外的初始化,那么与 init 方法无关。您只需调用:

B *someBObject = [[B alloc] initWithName:@"A Name"];

这将创建 B 对象并从 A 调用 initWithName: 方法。

现在,如果您的 B class 需要在 initWithName: 方法中进行补充工作,那么您将其添加到 B.m:

- (instancetype)initWithName:(NSString *)name {
    self = [super initWithName:name];
    if (self) {
        // do some additional stuff to initialize this "B" instance
    }

    return self;
}