Is it possible to prevent all children of a child class from accessing a variable in a base class in Swift 5? Please no round-about Protocol solutions.
class A { var a:String = ""}
class B : A {protected var a = ""}
class C : B
{
func use_a()
{
a = //compiler should complain here that 'a' is protected and cannot be modified
}
}
CodePudding user response:
It is possible, but only by scoping members using file structure.
File 1:
class A {
fileprivate var a = ""
}
class B: A {
fileprivate override var a: String {
willSet { }
}
}
File 2:
class C: B {
var a = 867_5309
func use_a() {
super.a // 'a' is inaccessible due to 'fileprivate' protection level
}
}