【发布时间】:2020-11-09 19:08:24
【问题描述】:
我正在尝试使用 MVVM 输入输出方法来实现 VC,但是当我尝试在视图控制器中绑定重新分配输出闭包时遇到编译器错误。我明白“输出”是一个只能获取的属性,但它的属性是被设置的。
我该如何解决这个问题?
protocol ViewModelOutputsType {
var didReceiveServiceError: ((Error) -> Void) { get set }
var reloadData: (() -> Void) { get set }
}
protocol ViewModelType {
var outputs: ViewModelOutputsType { get }
}
final class ViewModel: ViewModelType, ViewModelOutputsType {
var outputs: ViewModelOutputsType { return self }
//output
var didReceiveServiceError: ((Error) -> Void) = { _ in }
var reloadData: (() -> Void) = {}
}
class ViewController: UIViewController {
var viewModel: ViewModel
init(viewModel: ViewModel) {
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)
}
private func bind() {
//this gives the compiler error
viewModel.outputs.didReceiveServiceError = { [weak self] error in
}
//this gives the compiler error
viewModel.outputs.reloadData = { [weak self] in
}
}
}
【问题讨论】:
-
请包含
SearchViewModelOutputsType -
你包含
ViewModelOutputsType协议 -
谢谢你,这只是一个错字,它的目的是到处都是 ViewModelOutputsType。