Protocol Swift Property in the Protocol - Candidate has an inconsistent type

I have a protocol (ProtocolA) containing one property corresponding to the second protocol (ProtocolB).

public protocol ProtocolA {        
    var prop: ProtocolB? { get }
}

public protocol ProtocolB {        
}

I am trying to declare two classes that will implement them:

private class ClassA : ProtocolA {    
    var prop: ClassB?
}

private class ClassB : ProtocolB {
}

But I get an error message:

Type 'ClassA' does not conform to protocol 'ProtocolA'

A protocol requires a prop property of type ProtocolB?

Candidate has inconsistent type "ClassB?"

Which is annoying because ClassB conforms to protocol B.

in the good old, I would just declare the property as:

@property (nonatomic) ClassB <ProtocolB> *prop;

but the only way, apparently, I can get around this quickly by adding ivar as:

private class ClassA : ProtocolA {        
    var _prop: ClassB?
    var prop: ProtocolB? { return _prop }
}

Is there no way around this?

+4
1

typealias , . , , , prop ProtocolB, , , .

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

protocol ProtocolB {}


class ClassA : ProtocolA {
    var prop: ClassB?
}

class ClassB : ProtocolB {}
+2

Source: https://habr.com/ru/post/1613257/


All Articles