ReactiveSwift 简单使用

记录 ReactiveSwift 简单使用

  1. 导入 ReactiveSwift 库
  2. 创建 TestViewModel 文件
c 复制代码
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
    }
    
}
  1. 使用 TestViewModel
c 复制代码
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()
    }
相关推荐
coooliang4 天前
【iOS】SwiftUI状态管理
ios·swiftui·swift
席子哥哥的代码库5 天前
自制简单的图片查看器(python)
开发语言·python·swift
打工人你好6 天前
Swift 的 KeyPath 是什么?
swift
struggle20256 天前
Ollmao (OH-luh-毛程序包及源码) 是一款原生 SwiftUI 应用程序,它与 Ollama 集成,可在 Mac 上本地运行强大的 AI 模型
ios·swiftui·swift
神仙别闹8 天前
基于Swift实现拼图游戏
开发语言·搜索引擎·swift
神仙别闹9 天前
基于Swift实现仿IOS闹钟
ios·cocoa·swift
ctf_022613 天前
echarts 3d中国地图飞行线
3d·echarts·swift
没头脑的ht14 天前
UITableView的复用原理
ios·swift
自不量力的A同学16 天前
苹果公司宣布正式开源 Xcode 引擎 Swift Build145
swift
SchneeDuan16 天前
iOS--SDWebImage源码解析
ios·缓存·swift·第三方库·sdwebimage