0 votes
in ios by (190 points)

2 Answers

0 votes
by (290 points)
Write the required modifier before the definition of a class initializer to indicate that every subclass of the class must implement that initializer.
0 votes
by (380 points)

It's actually just a way of satisfying the compiler to assure it that if this class were to have any subclasses, they would inherit or implement this same initializer. There is doubt on this point, because of the rule that if a subclass has a designated initializer of its own, no initializers from the superclass are inherited. Thus it is possible for a superclass to have an initializer and the subclass not to have it. required overcomes that possibility.

One situation where the compiler needs to be satisfied in this way involves protocols, and works like this:

protocol Flier {

    init()

}

class Bird: Flier {

    init() {} // compile error

}

The problem is that if Bird had a subclass, that subclass would have to implement or inherit init, and you have not guaranteed that. Marking Bird's init as required does guarantee it.

Alternatively, you could mark Bird as final, thus guaranteeing the converse, namely that it will never have a subclass.

Another situation is where you have a factory method that can make a class or its subclass by calling the same initializer:

class Dog {

    var name: String

    init(name: String) {

        self.name = name

    }

}

class NoisyDog: Dog {

}

func dogMakerAndNamer(whattype: Dog.Type) -> Dog {

    let d = whattype.init(name: "Fido") // compile error

    return d

}

dogMakerAndNamer is calling the init(name:) initializer on Dog or a Dog subclass. But how can the compiler be sure that a subclass will have an init(name:) initializer? The required designation calms the compiler's fears.

Welcome to Reubro Q&A, where you can ask questions and receive answers from other members of the community.
...