【发布时间】:2018-03-27 22:12:54
【问题描述】:
我正在尝试在我的应用程序中使用通用协议。我尝试使用的代码是这样的。
protocol BaseProtocol {
associatedtype PresenterType
var presenter: PresenterType? { get set }
}
protocol PresenterProtocol {
associatedtype View
var view: View? { get set }
}
protocol ChildPresenter: PresenterProtocol where View == ChildProtocol {
}
protocol ChildProtocol: BaseProtocol where PresenterType == ChildPresenter {
}
class A: ChildProtocol {
var presenter: ChildPresenter?
}
编译器在符合 ChildProtocol 时抛出错误
错误:协议“ChildPresenter”只能用作泛型 约束,因为它具有 Self 或关联的类型要求 var 主持人:ChildPresenter?
当我已经清除了 associateType 时,我不明白为什么编译器会抛出此错误。
【问题讨论】:
-
如果您不明白为什么会出错,我在回答中添加了解释。