RxSwift using bindTo to bind Variable <String> to UILabel not working for Swift 3.0 update

I use RxSwift to bind my viewmodel to UILabel and UITexfield . UITextfield they have no problems converting to Swift 3 , only replacing rx_text with rx.text .

But not for UILabel . On Swift 2.2 I used:

 self.viewModel.shiftNameText.asObservable().bindTo(self.shiftLabel.rx_text).addDisposableTo(self.disposeBag) 

In Swift 3 I use RxSwift 3.0.0-beta.1 and try to just change rx_text to rx.text , but it does not compile and does not show this error "Cannot convert value like ' AnyObserver<String?>' (aka 'AnyObserver<Optional<String>>') to expected argument type 'Variable<String>".

Does anyone know why and how to make this work? Thanks.

+7
ios swift swift3
source share
1 answer

UILabel rx.text The property is of type AnyObserver<String?> So you need to map the value to the optional

 self.viewModel.shiftNameText .asObservable() .map { text -> String? in return Optional(text) } .bindTo(self.shiftLabel.rx.text) .addDisposableTo(self.disposeBag) 

or briefly:

 self.viewModel.shiftNameText .asObservable() .map { $0 } .bindTo(self.shiftLabel.rx.text) .addDisposableTo(self.disposeBag) 

See https://github.com/ReactiveX/RxSwift/issues/875 for other solutions.

+19
source share

All Articles