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()
    }
相关推荐
core51217 小时前
使用 `ms-swift` 微调 Qwen3-VL-2B 详细指南
lora·微调·swift·qwen·qwen3·vl
core51218 小时前
Swift SFT Qwen-VL LoRA 微调指令详解
lora·微调·swift·qwen·vl
Swift社区2 天前
LeetCode 375 - 猜数字大小 II
算法·leetcode·swift
Swift社区3 天前
使用 MetricKit 监控应用性能
ios·swiftui·swift
Swift社区3 天前
LeetCode 374 猜数字大小 - Swift 题解
算法·leetcode·swift
七牛云行业应用3 天前
iOS 19.3 突发崩溃!Gemini 3 导致 JSON 解析失败的紧急修复
人工智能·ios·swift·json解析·大模型应用
初级代码游戏3 天前
iOS开发 SwiftUI 6 :List
ios·swiftui·swift
特别橙的橙汁4 天前
Node.js 调用可执行文件时的 stdout 缓冲区问题
前端·node.js·swift
牛马1114 天前
iOS swift 自定义View
ios·cocoa·swift
牛马1114 天前
ios swift处理json数据
ios·json·swift