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()
    }
相关推荐
TT_Close2 天前
【Flutter×鸿蒙】FVM 不认鸿蒙 SDK?4步手动塞进去
flutter·swift·harmonyos
张江2 天前
Swift Concurrency学习
swift
东坡肘子4 天前
OpenClaw 不错,但我好像没有那么需要 -- 肘子的 Swift 周报 #125
人工智能·swiftui·swift
Swift社区9 天前
LeetCode 391 完美矩形 - Swift 题解
算法·leetcode·swift
升讯威在线客服系统10 天前
从 GC 抖动到稳定低延迟:在升讯威客服系统中实践 Span 与 Memory 的高性能优化
java·javascript·python·算法·性能优化·php·swift
Swift社区10 天前
LeetCode 390 消除游戏 - Swift 题解
leetcode·游戏·swift
东坡肘子11 天前
春晚、机器人、AI 与 LLM -- 肘子的 Swift 周报 #124
人工智能·swiftui·swift
BatmanWayne14 天前
swift-微调补充
人工智能·swift
疯笔码良18 天前
【swiftUI】实现自定义的底部TabBar组件
ios·swiftui·swift
东坡肘子19 天前
祝大家马年新春快乐! -- 肘子的 Swift 周报 #123
人工智能·swiftui·swift