I use RxSwift
to bind my viewmodel
to UILabel
and UITexfield
. UITextfield
ones have no issues converting to Swift 3
, just replacing rx_text
with rx.text
worked.
But not for UILabel
. On Swift 2.2
, I used:
self.viewModel.shiftNameText.asObservable().bindTo(self.shiftLabel.rx_text).addDisposableTo(self.disposeBag)
For Swift 3
, I'm using RxSwift
3.0.0-beta.1 and tried just changing rx_text to rx.text, but it doesn't compile and shows this error "Cannot convert value of type 'AnyObserver<String?>' (aka 'AnyObserver<Optional<String>>') to expected argument type 'Variable<String>".
Does anyone know why and how to make this work? Thanks.
UILabel
's rx.text
property is of type AnyObserver<String?>
so you need to map the value to an optional
self.viewModel.shiftNameText
.asObservable()
.map { text -> String? in
return Optional(text)
}
.bind(to:self.shiftLabel.rx.text)
.disposed(by:self.disposeBag)
or in short:
self.viewModel.shiftNameText
.asObservable()
.map { $0 }
.bind(to:self.shiftLabel.rx.text)
.disposed(by:self.disposeBag)
See https://github.com/ReactiveX/RxSwift/issues/875 for other solutions.
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