RxSwift How to bind and bind UITextView text to the text field of a custom object?

I know that I can do the following to map the changes to the text field of a custom Notes object in a UITextView.

self.notesViewModel.currentNote() .map { $0.text } .bindTo(self.notesTextView.rx_text) 

How can I do the opposite using the same template? The template is notesTextView.rx_text, match it with the text of the current note. I know how to do the following that non-RxSwift-y feels:

 _ = notesTextView.rx_text.subscribeNext { someText in self.notesViewModel.currentNote().value.text = someText } 

IOW, it looks like I can take a UITextView (aka notesTextView), a map and bind the changes to the current note. The current note returns a variable with a note that has a String text field.

+7
swift rx-swift
source share
2 answers

You can use the two-way binding operator <-> .

In a good example, the RxSwift repository provides an example of use:

 // bind UI values to variables usernameOutlet.rx_text <-> username passwordOutlet.rx_text <-> password repeatedPasswordOutlet.rx_text <-> repeatedPassword 
+11
source share

I needed to do this between a textField and a variable of heterogeneous types, I presented a solution that I came up with using tuples and closures as a transfer request:

https://github.com/ReactiveX/RxSwift/pull/559

The same method can be used to match a double line, for example.

0
source share

All Articles