如果全部都不为空,则组合文本字段结果 RxSwift

Posted

技术标签:

【中文标题】如果全部都不为空,则组合文本字段结果 RxSwift【英文标题】:combined textfields result if all are not empty RxSwift 【发布时间】:2019-03-02 07:35:13 【问题描述】:

我正在尝试合并所有文本字段的输入并检查它是否有输入,但在观察时合并最新只订阅了一次。

是否有其他方法可以使用 rxswift 检查文本字段是否为空?那些 OTP 文本字段

let otp1Value: Observable<Bool> = self.otp1.rx.controlEvent(UIControlEvents.editingDidEnd)
            .withLatestFrom(self.otp1.rx.text)
            .map !$0!.isEmpty 
            .share()

        let otp2Value: Observable<Bool> = self.otp1.rx.controlEvent(UIControlEvents.editingDidEnd)
            .withLatestFrom(self.otp2.rx.text)
            .map !$0!.isEmpty 
            .share()

        let otp3Value: Observable<Bool> = self.otp1.rx.controlEvent(UIControlEvents.editingDidEnd)
            .withLatestFrom(self.otp3.rx.text)
            .map !$0!.isEmpty 
            .share()

        let otp4Value: Observable<Bool> = self.otp1.rx.controlEvent(UIControlEvents.editingDidEnd)
            .withLatestFrom(self.otp4.rx.text)
            .map !$0!.isEmpty 
            .share()

        let otp5Value: Observable<Bool> = self.otp1.rx.controlEvent(UIControlEvents.editingDidEnd)
            .withLatestFrom(self.otp5.rx.text)
            .map !$0!.isEmpty 
            .share()

        let otp6Value: Observable<Bool> = self.otp1.rx.controlEvent(UIControlEvents.editingDidEnd)
            .withLatestFrom(self.otp6.rx.text)
            .map !$0!.isEmpty 
            .share()

        Observable.combineLatest(
            otp1Value.asObservable(),
            otp2Value.asObservable(),
            otp3Value.asObservable(),
            otp4Value.asObservable(),
            otp5Value.asObservable(),
            otp6Value.asObservable())
            .asObservable().subscribe(onNext:  [weak self] (arg: (Bool, Bool, Bool, Bool, Bool, Bool)) -> Void in

                guard let self = self else  return 

                print("args \(arg)")

                switch arg 
                case (true, true, true, true, true, true):
                    self.step2CellViewModel.otpIsValid.onNext(true)
                    print("args \(arg)")
                default:
                    self.step2CellViewModel.otpIsValid.onNext(false)
                    print("args false")
                
            )
            .disposed(by: self.disposeBag) 

【问题讨论】:

您只是要求进行代码审查还是遇到了问题? 【参考方案1】:

这也应该有效:

let textFields = [otp1, otp2, otp3, otp4, otp5, otp6]
Observable.combineLatest(textFields.map  $0!.rx.text.orEmpty )
    .map  $0.map  $0.isEmpty  
    .map  !$0.contains(true) 
    .bind(to: step2CellViewModel.otpIsValid)
    .disposed(by: disposeBag)

【讨论】:

【参考方案2】:

已经找到答案了:

 let valids: [Observable<Bool>] = [
                self.otp1, self.otp2,
                self.otp3, self.otp4,
                self.otp5, self.otp6
            ].map  field in

            field.rx.text.map( _ in return !(field.text?.isEmpty)! )
        

        _ = Observable.combineLatest(valids)  iterator -> Bool in
            return iterator.reduce(true,  return $0 && $1 )
        .subscribe(onNext:  [weak self] (valid: Bool) -> Void in
            guard let self = self else  return 
            self.step2CellViewModel.otpIsValid.value = valid
        )
        .disposed(by: self.disposeBag) 

【讨论】:

以上是关于如果全部都不为空,则组合文本字段结果 RxSwift的主要内容,如果未能解决你的问题,请参考以下文章

Symfony 表单验证:如果另一个字段不为空,则需要字段

SwiftUI:仅当输入不为空时才启用保存按钮

如果 TextField 为空,则设置自定义文本

如果特定列不为空,则 Google 表格导入范围

如果字段不为空,则添加对验证器的检查

c#winforms中所有文本框都不为空后如何启用按钮?