记录 ReactiveSwift 简单使用
- 导入 ReactiveSwift 库
- 创建 TestViewModel 文件
enum JKTypeType: Int {
case cloud
case device
case weater
}
// 通过监听属性变化
class TestViewModel: NSObject {
lazy var recordType: Property<JKTypeType> = {
return Property(recordTypeProperty)
}()
private(set) var recordTypeProperty: MutableProperty<JKTypeType> = MutableProperty(.device)
deinit {
print("TestViewModel delloc")
}
}
extension TestViewModel {
func setRecordType(_ type: JKTypeType) {
recordTypeProperty.value = type
}
}
- 使用 TestViewModel
class RegisterVC: UIViewController {
var testViewModel = TestViewModel()
let disposes: CompositeDisposable = CompositeDisposable()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
bindViewModel()
}
func bindViewModel() {
disposes += testViewModel.recordType.signal.observeValues { type in
print("testViewModel: \(type)")
}
}
@IBAction func backClick(_ sender: Any) {
testViewModel.setRecordType(.cloud)
}
}
deinit {
disposes.dispose()
}