我遇到了以下代码,不确定为什么不编译:
protocol CellDelegate: class {}
protocol DelegatingCellViewModel {
var delegate: CellDelegate? { get }
}
protocol ProductCellViewModelDelegate: CellDelegate {}
// Error: Type 'ProductCellViewModel' does not conform to protocol 'DelegatingCellViewModel'
class ProductCellViewModel: DelegatingCellViewModel {
weak var delegate: ProductCellViewModelDelegate?
}
完整错误消息:
error: Playground.playground:9:7: error: type 'ProductCellViewModel' does not conform to protocol 'DelegatingCellViewModel'
class ProductCellViewModel: DelegatingCellViewModel {
^
Playground.playground:10:14: note: candidate has non-matching type 'ProductCellViewModelDelegate?'
weak var delegate: ProductCellViewModelDelegate?
^
Playground.playground:4:9: note: protocol requires property 'delegate' with type 'CellDelegate?'; do you want to add a stub?
var delegate: CellDelegate? { get }
^
这是语言限制吗?还是我缺少什么? 应该如何编写此代码,以便对其进行编译并保持其意图?
答案 0 :(得分:0)
您需要更改委托的类型或添加协议存根。
class ProductCellViewModel: DelegatingCellViewModel {
weak var delegate: CellDelegate?
}
或 您必须像这样更改该特定委托的名称。因为委托名称已经被符合协议使用-
class ProductCellViewModel: DelegatingCellViewModel {
var delegate: CellDelegate?
weak var productDelegate: ProductCellViewModelDelegate?
}