I have this code:
public protocol MyProtocol {
init()
}
public extension MyProtocol {
public init() {
self.init()
}
}
public final class MyClass: MyProtocol {}
I got an error saying:
Initializer 'init()' must be declared public because it matches a requirement in public protocol 'MyProtocol'
If I remove the access control (public
) before final
, it works. But why? Is there any way I can let the protocol handle the init? I thought all members of protocols are implicitly public
by default.
More strange is that a different init
that can only be found in the extension
is public
by default:
public protocol MyProtocol {
init()
}
public extension MyProtocol {
public init() {
self.init()
}
public init(youDoNotHaveToImplementMe: Any) {
self.init()
}
}
public final class MyClass: MyProtocol {
public init() {}
}
As you can see, the new init is actually public
. I expected my normal init
should be public
as well. Why is this not the case?