O'Reilly logo

Reactive Programming with Swift 4 by Navdeep Singh

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Functional programming to the rescue

We will use the map function on the control property to filter out values as follows:

usernameTextField.rx.text.map { $0 ?? "" }

$0 represents the first variable, and if this is nil, we will return an empty string and pass that through to bind the returned value to the ViewModel's variable as desired by making use of the bindTo operator:

usernameTextField.rx.text.map { $0 ?? ""        .bind(to: loginViewModel.username)

This will simply tell the ViewModel that the input of this text field has changed. The preceding line of code might throw a warning about an unused value.

To silence the warning, add _ =.
This can be resolved as follows:
_ = usernameTextField.rx.text.map { $0 ?? "" } .bind(to: loginViewModel.username) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required