2
protocol BasePresenterProtocol : class {} protocol DashboardPresenterProtocol : BasePresenterProtocol {} final class DashboardPresenter { weak var view: DashboardPresenterProtocol? init() { self.view = DashboardViewController() } func test() { print("Hello") } } extension DashboardPresenter: DashboardViewProtocol { } protocol BaseViewProtocol : class { weak var view: BasePresenterProtocol? { get set } } protocol DashboardViewProtocol : BaseViewProtocol { } class DashboardViewController { } extension DashboardViewController: DashboardPresenterProtocol { } 

In the above code, I get an error at following line

extension DashboardPresenter: DashboardViewProtocol { } 

that, DashboardPresenter doesn't confirm to protocol DashboardViewProtocol, but I have declared weak var view: DashboardPresenterProtocol? in DashboardPresenter . Although I have declared

Why am I getting this error ? Please let me know what I am doing wrong in this code.

0

1 Answer 1

6

You cannot implement a read-write property requirement of type BasePresenterProtocol? with a property of type DashboardPresenterProtocol?.

Consider what would happen if this were possible, and you upcast an instance of DashboardPresenter to DashboardViewProtocol. You would be able to assign anything that conforms to BasePresenterProtocol to a property of type DashboardPresenterProtocol? – which would be illegal.

For this reason, a read-write property requirement has to be invariant (although it's worth noting that a readable-only property requirement should be able to be covariant – but this currently isn't supported).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.