I am trying to understand the use of the required
keyword in Swift classes.
class SomeClass
{
required init() {
// initializer implementation goes here
}
}
required
doesn't force me to implement the method in my child-class. If I want to override the required
designated initializer of my parent class I need to write required
and not override
. I know how it works but can not understand why I should do this.
What is the benefit of required
?
As far as I can tell, languages like C# don't have something like this and work just fine with override
.
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.