CombineOperators
描述
这个仓库包括 Combine 的操作符,几乎完全重实现了 RxCocoa、RxViewController、RxGestures 和 RxKeyboard 库以及一些附加功能。
示例
import UIKit
import Combine
import CombineCocoa
import CombineOperators
final class SomeViewModel {
let title = CurrentValueSubject<String, Never>("Title")
let icon = CurrentValueSubject<UIImage?, Never>(nil)
let color = CurrentValueSubject<UIColor, Never>(UIColor.white)
let bool = CurrentValueSubject<Bool, Never>(false)
...
}
class ViewController: UIViewController {
@IBOutlet private weak var titleLabel: UILabel!
@IBOutlet private weak var iconView: UIImageView!
@IBOutlet private weak var switchView: UISwitch!
let viewModel = SomeViewModel()
...
private func configureSubscriptions() {
viewModel.title ==> titleLabel.cb.text
viewModel.iconView ==> iconView.cb.image
viewModel.bool ==> switchView.cb.isOn
viewModel.color ==> (self, ViewController.setTint)
//or viewModel.color ==> cb.weak(method: ViewController.setTint)
//or viewModel.color ==> {[weak self] in self?.setTint(color: $0) }
}
private func setTint(color: UIColor) {
...
}
...
}使用
- 操作符
=>
-
从
Publisher到Subscriber,创建一个订阅intPublisher => intSubscriber
- From `Publisher` to `Subject`, creates a subscription and returns `Cancellable`:
```swift
let Cancellable = intPublisher => intSubject
- 从
Cancellable到DisposeBag
someCancellable => cancellableSet
somePublisher => someSubject => cancellableSet- 从
Publisher到Scheduler,返回AnyPublisher<Output, Failure>
somePublisher => DispatchQueue.main => someSubscriber- 从
Publisher到(Output) -> Void
somePublisher => { print($0) }- 从
Publisher到@autoescaping () -> Void
somePublisher => print("action")所有操作符尽可能将输出-输入类型和错误类型进行类型转换
- 操作符
==>
在主队列上驱动 Publisher 到 Subscriber
intPublisher ==> intSubscriber => cancellableSet-
操作符
=>>和==>>替代.removeDublicates() -
CancellableBuilder和MergeBuilder、CombineLatestBuilder- 结果构建器 -
一些功能
UIView.cb操作符:isVisible、willAppear、isOnScreen、didAppear、movedToWindow、frame、frameOnWindow等skipNil()操作符- 布尔序列的
or(Bool), .toggle(), !操作符 - 使用
+和+=操作符合并发布者、创建 Cancellables 等 interval(...)withLast()->.mp-@dynamicMemberLookup映射器asResult() -> AnyPublisher, Never> nilIfEmptyisEmptyisNilisNilOrEmptyappend(...)smooth(...)方法用于平滑变化,例如:序列[0, 1]转为[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]onValue, onFailure, onFinished, onSubscribe, onCancel, onRequest方法包装在handleEvents(...)操作符上guard()cb.isFirstResponderUIStackView().cb.update(...)UIView().cb.transform.scale(), .rotation(), .translation()
安装
CombineOperators可以通过CocoaPods获取。安装它,只需将以下行添加到您的Podfile中
pod 'CombineOperators/CombineCocoa'然后,首先从Podfile目录运行pod update
创建一个Package.swift文件。
// swift-tools-version:5.0
import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/CombineOperators.git", from: "1.73.0")
],
targets: [
.target(name: "SomeProject", dependencies: ["CombineOperators"])
]
)$ swift build作者
Voidilov, [email protected]
许可证
CombineOperators在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。