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.
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).
来源:https://stackoverflow.com/questions/40410884/swift-protocol-inheritance-and-protocol-conformance-issue