Final required initializer of open class

Hello all!

I am working on a project and have a class that is meant to be subclassed. So it is defined with the open access modifier. However, I also want it to conform to some protocols that have required initializers (e.g. ExpressibleByNilLiteral).

I want the base class's implementation of this initializer to stand for all subclasses. However, every time I subclass, the compiler tells me I need to implement that same initializer. So I end up doing something like this:

    public required init(nilLiteral: ()) {
        super.init(nilLiteral: ())
    }

Is this just a limitation of the Swift language, or is there a way to avoid having to do the above for each subclass?

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/initialization#Automatic-Initializer-Inheritance

2 Likes

Thank you! I forgot the rules were that simple (and also strict, I guess).

1 Like