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()
    }
相关推荐
碎_浪1 小时前
Mac 壁纸被 MDM 锁住?我做了个 2MB 的开源方案
macos·开源·swift
小牛不牛的程序员1 天前
开源一个 macOS 剪贴板「意图识别」工具——不只是存储,更是理解你复制了什么
macos·swift
apihz2 天前
经纬度制作高清卫星图片免费 API 接口详解
开发语言·ios·swift
00后程序员张3 天前
iOS 打包方式汇总 从 Xcode Archive 到轻量级工具链的 IPA 构建
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
sweet丶3 天前
Swift 黑魔法:`@dynamicMemberLookup`
swift·rxswift
大熊猫侯佩4 天前
SwiftUI 侦探笔记:iPhone 14 失踪的“刘海”与侯佩的像素诡计
swiftui·swift·apple
大熊猫侯佩4 天前
WWDC26 新 @ContentBuilder 修饰符:SwiftUI 终于有了户口本
swiftui·swift·wwdc
东坡肘子4 天前
当 Linux 成为“空气”:容器、Agent 与不再重要的“桌面之争” -- 肘子的 Swift 周报 #143
人工智能·swiftui·swift
wabil6 天前
【LVGL】滑动切换页面的界面优化实践
开发语言·ios·swift
东坡肘子11 天前
SPI 加入 Apple,Swift 迈向自举 -- 肘子的 Swift 周报 #142
人工智能·swiftui·swift