I am writing from 2023 so maybe there are some changes. So, we have access for private let propertyB in extension ClassB. So go search more, but for me question is still actives
Edited with new information:
The is better example. Imagine we have
class Shape {
var backgroundColor: UIColor?
init(backgroundColor: UIColor?) {
self.backgroundColor = backgroundColor
}
}
class Rectangle: Shape {
var width: Int = 0
var height: Int = 0
}
As Rectangle does not have any initializers, it inherits the init(backgroundColor:) from the Shape and can be instantiated as below:
let rectangle = Rectangle(backgroundColor: UIColor.green)
Now imagine we could add designated initializers by extensions like this:
extension Rectangle {
init(backgroundColor: UIColor?, width: Int, height: Int) {
self.width = width
self.height = height
super.init(backgroundColor: backgroundColor)
}
}
From now on, the Rectangle class Will have a designated initializer for its own, hence, the Swift will remove the inherited initializer init(backgroundColor:) from the Rectangle. So what will happened to the codes that used the inherited initializer to instantiate the Rectangle. All of them would be invalid. Now suppose the Shape and Rectangles are classes that have been defined in one of the Apple frameworks. Good job. We managed to make Apple frameworks invalid. Therefore, it is reasonable that Swift does not allow us add designated initializers using the extensions.
There question is still Active:
We can add designated init for Struct. And it will destroy default memberwise initializer.
Silly example:
struct FirstStruct {
var prop1: Int
private var prop2: Int
}
extension FirstStruct {
init(prop: Int) {
self.prop1 = prop
self.prop2 = prop
}
}
Now FirstStruct does not have FirstStruct(prop1: Int, prop2: Int) initialiser.
prop2
is private. (Remove the extension and try to use it.) – Oration