I am willing to use RxSwift for MVVM binding between model values & view controllers. I wanted to follow this realm.io tutorial, 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:
LoginViewModel.swift
import RxSwift
struct LoginViewModel {
var username = Variable<String>("")
var password = Variable<String>("")
var isValid : Observable<Bool>{
return Observable.combineLatest(self.username.asObservable(), self.password.asObservable())
{ (username, password) in
return username.characters.count > 0
&& password.characters.count > 0
}
}
}
LoginViewController.swift
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)
}
}
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.
The first issue is with this binding, which does not compile:
usernameTextField.rx.text.bindTo(viewModel.username).addTo(disposeBag)
The error:
LoginViewController.swift:15:35: Cannot invoke 'bindTo' with an argument list of type '(Variable<String>)'
I've tried the following without luck:
1) usernameTextField.rx.text.bind(to: viewModel.username).addTo(disposeBag)
- The error still persists:
LoginViewController.swift:15:35: Cannot invoke 'bind' with an argument list of type '(to: Variable<String>)'
2) let _ = viewModel.username.asObservable().bind(to: passwordTextField.rx.text)
let _ = viewModel.username.asObservable()
.map { $0 }
.bind(to: usernameTextField.rx.text)
This second one actually compiles, but does not work (ie. viewModel.username does not change)
The main problem is here that I am shooting blind when passing parameters to the bind
and bind(to:
methods, since the autocompletion is not really helpful here.. I am using swift 3 and Xcode 8.3.2.