I am new to RxSwift and I want to achieve this:
A titleTextField,a descTextField,a confirmButton and a submitButton.
The submitButton is unEnable until titleTextField and descTextField's text.count >= 5 and confirmButton is selected, so I write these code:
let titleValid = titleTextField.rx.text.orEmpty.map { (text) -> Bool in
return text.count >= 5
}
let descValid = descTextField.rx.text.orEmpty.map { (text) -> Bool in
return text.count >= 5
}
let isConfirm = confirmButton.rx.isSelected.asObserver().mapObserver { (selected) -> Bool in
return selected
}
Observable.combineLatest(titleValid, descValid, isConfirm) { $0 && $1 && $2 }.bind(to: submitButton.rx.isEnabled).disposed(by: disposeBag)
It's work good with titleValid
and descValid
,however,there is an error with isConfirm
:
Argument type 'AnyObserver<Bool>' does not conform to expected type 'ObservableType'
How can I change isConfirm
to ObservableType
? How to Correct it?
Note the RxSwift's version is 5.0.0
The comments contains your answer. It's the tapping of the confirmButton
that's important.
let titleValid = titleTextField.rx.text.orEmpty.map { (text) -> Bool in
return text.count >= 5
}
let descValid = descTextField.rx.text.orEmpty.map { (text) -> Bool in
return text.count >= 5
}
let isConfirm = confirmButton.rx.tap
.scan(false) { current, _ in !current }
.startWith(false)
isConfirm
.bind(to: confirmButton.rx.isSelected)
.disposed(by: disposeBag)
Observable.combineLatest(titleValid, descValid, isConfirm) { $0 && $1 && $2 }
.bind(to: submitButton.rx.isEnabled)
.disposed(by: disposeBag)