title: UITextField binding to ViewModel with RxSwift
body: <p>I am willing to use RxSwift for MVVM binding between model values &amp; view controllers. I wanted to follow this <a href="https://news.realm.io/news/slug-max-alexander-mvvm-rxswift/" rel="noreferrer">realm.io tutorial</a>, but the binding has apparently changed since then, and the sample code does not compile. Here is the sample code, where I think I've fixed the worst typos / missing things:</p>
  
  <p>LoginViewModel.swift</p>
  
  <pre><code>import RxSwift
  
  struct LoginViewModel {
  
      var username = Variable&lt;String&gt;("")
      var password = Variable&lt;String&gt;("")
  
      var isValid : Observable&lt;Bool&gt;{
          return Observable.combineLatest(self.username.asObservable(), self.password.asObservable())
          { (username, password) in
              return username.characters.count &gt; 0
                  &amp;&amp; password.characters.count &gt; 0
          }
      }
  } 
  </code></pre>
  
  <p>LoginViewController.swift</p>
  
  <pre><code>import RxSwift
  import RxCocoa
  import UIKit
  
  class LoginViewController: UIViewController {
      var usernameTextField: UITextField!
      var passwordTextField: UITextField!
      var confirmButton: UIButton!
  
      var viewModel = LoginViewModel()
  
      var disposeBag = DisposeBag()
  
      override func viewDidLoad() {
          usernameTextField.rx.text.bindTo(viewModel.username).addTo(disposeBag)
          passwordTextField.rx.text.bindTo(viewModel.password).addTo(disposeBag)
  
          //from the viewModel
          viewModel.rx.isValid.map { $0 }
              .bindTo(confirmButton.rx.isEnabled)
      }
  }
  </code></pre>
  
  <p>The controller bindings do not compile. It is pretty close to impossible to track the correct way to do these, as the RxSwift documentation is pretty unhelpful, and the Xcode autocompletion does not suggest anything useful.</p>
  
  <p>The first issue is with this binding, which does not compile:
  <code>usernameTextField.rx.text.bindTo(viewModel.username).addTo(disposeBag)</code></p>
  
  <p>The error:</p>
  
  <p><code>LoginViewController.swift:15:35: Cannot invoke 'bindTo' with an argument list of type '(Variable&lt;String&gt;)'</code></p>
  
  <p>I've tried the following without luck:</p>
  
  <p>1) <code>usernameTextField.rx.text.bind(to: viewModel.username).addTo(disposeBag)</code> - The error still persists:
   <code>LoginViewController.swift:15:35: Cannot invoke 'bind' with an argument list of type '(to: Variable&lt;String&gt;)'</code></p>
  
  <p>2) 
      let _ = viewModel.username.asObservable().bind(to: passwordTextField.rx.text)</p>
  
  <pre><code>let _ = viewModel.username.asObservable()
              .map { $0 }
              .bind(to: usernameTextField.rx.text)
  </code></pre>
  
  <p>This second one actually compiles, but does not work (ie. viewModel.username does not change) </p>
  
  <p>The main problem is here that I am shooting blind when passing parameters to the <code>bind</code> and <code>bind(to:</code> methods, since the autocompletion is not really helpful here.. I am using swift 3 and Xcode 8.3.2.</p>