我想通过使用Rxswift得到一个Observable的UIImageView是空的.
这是我的代码:
let usernameValid = firstTextField.rx.text.orEmpty .map { $0.characters.count >= 1 } .shareReplay(1) let passwordValid = secondTextField.rx.text.orEmpty .map { $0.characters.count >= 1 } .shareReplay(1) let everythingValid = Observable.combineLatest(usernameValid, passwordValid) { $0 && $1 } .shareReplay(1) everythingValid.bindTo(submitButton.rx.isEnabled).addDisposableTo(disposeBag)
我想得到像usernameValid类似的Observable.
该.rx.image
属性上UIImageView
的类型的UIBindingObserver
.此类型未实现ObservableType
,因此您将无法简单地编写imageView.rx.image.orDefault(defaultImage)
.
如果你想观察a的image
属性UIImageView
,你需要使用KVO.
// this is "equivalent" to `firstTextField.rx.text` let imageObs: Observable= imageView.rx.observe(Optional .self, "image") // default image will act in the same way the empty string is used in the case of `orEmpty` let defaultImage = UIImage(named: "defaultImage")! let imageWithDefaultObs: Observable = imageObs.map { // return defaultImage when $0 is nil return $0 ?? defaultImage }