0

我正在阅读有关输入输出 ViewModel 方法的博客: https ://medium.com/blablacar-tech/rxswift-mvvm-66827b8b3f10

完整代码:https ://gist.github.com/MartinMoizard

我只是对如何做到感到困惑

let greeting = validateSubject
        .withLatestFrom(nameSubject)
        .map { name in
            return "Hello \(name)!"
        }
        .asDriver(onErrorJustReturn: ":-(")

https://gist.github.com/MartinMoizard/4d66528a9959cbbdefa6d50394d2bfb1

如果在https://gist.github.com/MartinMoizard/449be0d30920010210988f1773a2ca90中点击validateSubject是私有的, 则会触发

final class ButtonCell: UITableViewCell, SayHelloViewModelBindable {
    @IBOutlet weak var validateButton: UIButton!
    var disposeBag: DisposeBag?

    override func prepareForReuse() {
        super.prepareForReuse()
        disposeBag = nil
    }

    func bind(to viewModel: SayHelloViewModel) {
        let bag = DisposeBag()
        validateButton.rx
            .tap
            .bind(to: viewModel.input.validate)
            .disposed(by: bag)
        disposeBag = bag
    }
}

TIA

4

1 回答 1

1

他并没有真正访问validateSubjecttap但最终他创建了一些可访问的输入/输出层,例如:

struct Input {
   let name: AnyObserver<String>
   let validate: AnyObserver<Void>
} 

struct Output {
   let greeting: Driver<String>
}

与内部私有层/逻辑(例如validateSubjectand nameSubject)进行通信。

然后他将输入中的各种信息通过namevalidate期望从输出中返回信息greeting

于 2019-11-30T22:03:01.197 回答