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) ...

Get Reactive Programming with Swift 4 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.