测试测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布上次发布 | 2017年5月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Ivan 维护。
基于 ReactiveSwift 开发的,用于 Cocoa 框架的响应式扩展。
ReactiveSwift 提供了可组合的、声明性的和灵活的原始数据,这些原始数据围绕“价值随时间流的强大概念”构建。这些原始数据可用于一致地表示常见的 Cocoa 和通用编程模式,这些模式本质上是一种观察行为。
有关核心原始数据的更多信息,请参阅 ReactiveSwift。
ReactiveCocoa 使用 ReactiveSwift 中的声明性原始数据包装各种 Cocoa 框架的各个方面。
UI 绑定
UI 组件通过 <~
运算符公开 BindingTarget
,接受来自任何类型值流的绑定。
// Bind the `name` property of `person` to the text value of an `UILabel`.
nameLabel.reactive.text <~ person.name
注意:您需要导入 ReactiveSwift 才能使用 <~
运算符。
控制和用户交互
交互式 UI 组件公开 Signal
以供控制事件和控值在用户交互时更新。
一组精选的控件提供对 Action
的便捷、表达性绑定 API。
// Update `allowsCookies` whenever the toggle is flipped.
preferences.allowsCookies <~ toggle.reactive.isOnValues
// Compute live character counts from the continuous stream of user initiated
// changes in the text.
textField.reactive.continuousTextValues.map { $0.characters.count }
// Trigger `commit` whenever the button is pressed.
button.reactive.pressed = CocoaAction(viewModel.commit)
声明性的 Objective-C 动态性
创建由拦截 Objective-C 对象(例如方法调用拦截和对象析构)源的数据信号。
// Notify after every time `viewWillAppear(_:)` is called.
let appearing = viewController.reactive.trigger(for: #selector(UIViewController.viewWillAppear(_:)))
// Observe the lifetime of `object`.
object.reactive.lifetime.ended.observeCompleted(doCleanup)
表达性强且安全的 Key Path 观察
以 SignalProducer
和 DynamicProperty
的形式建立键值观察,并享受继承的可组合性。
// A producer that sends the current value of `keyPath`, followed by
// subsequent changes.
//
// Terminate the KVO observation if the lifetime of `self` ends.
let producer = object.reactive.values(forKeyPath: #keyPath(key))
.take(during: self.reactive.lifetime)
// A parameterized property that represents the supplied key path of the
// wrapped object. It holds a weak reference to the wrapped object.
let property = DynamicProperty<String>(object: person,
keyPath: #keyPath(person.name))
但是还有更多要发现和介绍。请阅读我们代码中的文档和发行说明来了解更多信息。
ReactiveCocoa 支持 macOS 10.9 及以上、iOS 8.0 及以上、watchOS 2.0 及以上和 tvOS 9.0 及以上。
git submodule update --init --recursive
。ReactiveCocoa.xcodeproj
、Carthage/Checkouts/ReactiveSwift/ReactiveSwift.xcodeproj
和 Carthage/Checkouts/Result/Result.xcodeproj
拖放到您的应用程序的 Xcode 项目或工作空间中。ReactiveCocoa.framework
、ReactiveSwift.framework
和 Result.framework
添加到“嵌入的二进制文件”部分。EMBEDDED_CONTENT_CONTAINS_SWIFT
构建设置为“是”。如需帮助,请访问我们的 GitHub 问题 或 Stack Overflow。如果您在存档中找不到任何解决方案,请随时创建一个问题。
当前稳定版本
该版本瞄准 Swift 3.1.x 和 ReactiveSwift 2.0。预计时间为 2017 年春季。
由于 ReactiveSwift 2.0 将包含破坏性变化,因此 ReactiveCocoa 的公共 API 也被认为有破坏性变化。
由于 Swift 4.0 将强制实施弹性,因此对我们来说,有一个干净和稳定的 API 是很重要的。预期在移动到 ReactiveSwift 3.0 和 Swift 4.0 之前,将在 ReactiveCocoa 6.0 中完成 API 审查。任何有助于实现这一目标的贡献都欢迎。
该版本瞄准 Swift 4.0.x 和 ReactiveSwift 3.0。预计时间为 2017 年晚些时候。
由于 ReactiveSwift 3.0 的变化,该版本可能包含破坏性变化。
ReactiveCocoa 7.0 将侧重于三个主要目标