I have a class:
internal class A {
}
I also have another class:
open class B: A {
}
I really need B to be open, but in order for this code to compile, I have to make A open, but making A open also makes it public and I don't want that.
Do you think if there's any way to make a class internal open?
CodePudding user response:
No, you can not, Swift doesn't support this scenario.
The design of the language involves making other overridable declarations in A to be visible in B in another module without explicitly redefining them. They need to be publicly visible/overrideable on A if they are to be visible on B and thus the entire class must be visible/overridable.
CodePudding user response:
This is probably the closest you can get to what you want:
open class A {
public let message: String
fileprivate init(message: String) {
self.message = message
}
}
open class B: A {
public override init(message: String) {
super.init(message: message)
}
public func printMessage() {
print(message)
}
}
Usage:
let b = B(message: "Hello world!")
b.printMessage()
Unfortunately, there doesn't seem to be a way to have the init in B inherited, because then it must be in A. But you also can't mark the init in A as @available(*, unavailable), otherwise it is unavailable to B too.
