【发布时间】:2016-10-14 09:35:54
【问题描述】:
我尝试建立各种协同工作的协议。不幸的是,我无法让它们按我想要的方式工作。看下面的代码,我想我的目标很明显:我想要一个符合协议 X 的类。如果它符合协议 Y,但协议 Y 继承自协议 X,它也应该被接受为符合协议的类.相反,我收到以下编译错误
Unable to infer associated type 'VC' for protocol 'ViewModelType'
Inferred type 'ExampleViewControllerType' (by matching requirement 'viewController') is invalid: does not conform to 'ViewType'
当前设置:
protocol ViewModelType: class {
associatedtype VC: ViewType
weak var viewController: VC! { get set }
}
class ExampleViewModel: ViewModelType {
weak var viewController: ExampleViewControllerType!
}
protocol ViewType: class { }
protocol ExampleViewControllerType: ViewType { }
class ExampleViewController: UIViewController, ExampleViewControllerType {
}
【问题讨论】:
-
Unable to use protocol as associatedtype in another protocol in Swift 的可能重复项。协议不符合自己,因此您不能将
ExampleViewControllerType用作符合ViewType的类型。
标签: ios swift protocols associated-types protocol-inheritance