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()
    }
相关推荐
FungLeo2 小时前
Swift macOS 游戏开发实战|空当接龙 07|使用 Swift 计算稳定牌桌布局与命中区域
macos·swift·游戏开发·界面布局·命中检测
FungLeo3 小时前
Swift macOS 游戏开发实战|空当接龙 06|使用 SVG 与 Node.js 制作 macOS 游戏牌面
macos·node.js·svg·swift·游戏素材
FungLeo1 天前
Swift macOS 游戏开发实战|空当接龙 03|使用 Swift 实现可复现的洗牌算法
macos·swift·随机数·fisher-yates·洗牌算法
东坡肘子1 天前
我的代码我做主 -- 肘子的 Swift 周报 #164
人工智能·swiftui·swift
布莱克6052 天前
Swift 中的逃逸闭包与非逃逸闭包:@escaping 到底在做什么
开发语言·ios·swift·闭包·逃逸
FungLeo2 天前
Swift macOS 开发实战 01|用 Swift 开发空当接龙,如何定义范围与验收
macos·项目管理·需求分析·swift·游戏开发
布莱克6052 天前
Swift 可选链详解:从语法到实战
开发语言·ios·swift·可选链
布莱克6052 天前
Swift 类型转换详解:从基础语法到实战技巧
开发语言·ios·swift·类型转换
FungLeo2 天前
Swift macOS 开发实战 02|SwiftUI 与 AppKit 混合开发,如何划分职责
macos·swiftui·swift·软件架构·appkit
布莱克6055 天前
Swift 自动引用计数(ARC)详解:原理、循环引用与最佳实践
开发语言·ios·swift·arc·自动引用计数