协议定义如下:
protocol BaseViewModelProtocol{
var goBack:(() -> Void)? { get set }
}
protocol MusicDetailViewModelProtocol:BaseViewModelProtocol{
}
viewModel定义如下:
class MusicDetailViewModel:MusicDetailViewModelProtocol{
var goBack: (() -> Void)?
}
当直接实现BaseViewModelProtocol
时,并不会报这个错
解决方案:
添加@objc标识,告诉编译器实现该protocol的对象是引用类型
@objc protocol BaseViewModelProtocol{
var goBack:(() -> Void)? { get set }
}
参考链接:
Cannot assign to property in protocol constrained to class - Swift compiler error