Swift protocol property in protocol - Candidate has non-matching type

前端 未结 1 1497
遇见更好的自我
遇见更好的自我 2021-01-19 12:35

I have a protocol (ProtocolA) containing a single property conforming to a second protocol (ProtocolB).

public protocol ProtocolA {        
    var prop: Prot         


        
1条回答
  •  粉色の甜心
    2021-01-19 13:06

    You need to declare a typealias of the type that conforms to the other protocol. The way you did it is that prop has to be exactly of type ProtocolB, but you don't actually want that, you want a type that conforms to it instead.

    protocol ProtocolA {
        typealias Prop : ProtocolB
        var prop: Prop? { get }
    }
    
    protocol ProtocolB {}
    
    
    class ClassA : ProtocolA {
        var prop: ClassB?
    }
    
    class ClassB : ProtocolB {}
    

    0 讨论(0)
提交回复
热议问题