I want to get a Observable of UIImageView is Empty by Using Rxswift.
Here is my code:
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)
I want get similar Observable like usernameValid.
The .rx.image property on UIImageView is of type UIBindingObserver. This type does not implement ObservableType, hence you will not be able to simply write imageView.rx.image.orDefault(defaultImage).
If you'd like to observe the image property of a UIImageView, you'll need to use KVO.
// this is "equivalent" to `firstTextField.rx.text`
let imageObs: Observable<UIImage?> = imageView.rx.observe(Optional<UIImage>.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<UIImage> = imageObs.map {
// return defaultImage when $0 is nil
return $0 ?? defaultImage
}
You can also write the isEmpty property as an extension to Reactive
extension Reactive where Base: UIImageView {
var isEmpty: Observable<Bool> {
return observe(UIImage.self, "image").map{ $0 == nil }
}
}
// Usage:
imageView.rx.isEmpty.subscribe(onNext: { isEmpty in
/** do something */
}).disposed(by: disposeBag)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With