I'm trying to make a complex class hierarchy conform to protocols, which help me to reuse code fragments. To that end, I face the following issue: class A has a property b, that is an instance of class B. B does conform to ProtocolB
. Now I want to define a protocol ProtocolA
, such that A does conform to ProtocolA
and that ProtocolA
specifies b of type ProtocolB
.
This is what I came up with:
class A {
var b: B?
}
class B {}
protocol ProtocolA {
var b: ProtocolB? { get }
}
protocol ProtocolB {}
extension B: ProtocolB {}
extension A: ProtocolA {}
Unfortunately, XCode shows the error:
Type 'A' does not conform to protocol 'ProtocolA'.
It is important to mention, that I can not directly define classes A and B like:
class A: ProtocolA {
var b: ProtocolB?
}
class B: ProtocolB {}
since the classes A and B are predefined by a library.