CombineOperators 1.73.0

CombineOperators 1.73.0

dankinsoid 维护。



  • 作者
  • Voidilov

CombineOperators

CI Status Version License Platform

描述

这个仓库包括 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) {
  ...
  } 
  ...
}

使用

  1. 操作符 =>
  • PublisherSubscriber,创建一个订阅

    intPublisher => intSubscriber

- From `Publisher` to `Subject`, creates a subscription and returns `Cancellable`:

```swift
  let Cancellable = intPublisher => intSubject
  • CancellableDisposeBag
someCancellable => cancellableSet
somePublisher => someSubject => cancellableSet
  • PublisherScheduler,返回 AnyPublisher<Output, Failure>
somePublisher => DispatchQueue.main => someSubscriber
  • Publisher(Output) -> Void
somePublisher => { print($0) }
  • Publisher@autoescaping () -> Void
somePublisher => print("action")

所有操作符尽可能将输出-输入类型和错误类型进行类型转换

  1. 操作符 ==>

在主队列上驱动 PublisherSubscriber

intPublisher ==> intSubscriber => cancellableSet
  1. 操作符 =>>==>> 替代 .removeDublicates()

  2. CancellableBuilderMergeBuilderCombineLatestBuilder - 结果构建器

  3. 一些功能

  • UIView.cb 操作符:isVisiblewillAppearisOnScreendidAppearmovedToWindowframeframeOnWindow
  • skipNil() 操作符
  • 布尔序列的 or(Bool), .toggle(), ! 操作符
  • 使用 ++= 操作符合并发布者、创建 Cancellables 等
  • interval(...)
  • withLast() ->
  • .mp - @dynamicMemberLookup 映射器
  • asResult() -> AnyPublisher, Never>
  • nilIfEmpty
  • isEmpty
  • isNil
  • isNilOrEmpty
  • append(...)
  • 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.isFirstResponder
  • UIStackView().cb.update(...)
  • UIView().cb.transform.scale(), .rotation(), .translation()

安装

  1. CocoaPods

CombineOperators可以通过CocoaPods获取。安装它,只需将以下行添加到您的Podfile中

pod 'CombineOperators/CombineCocoa'

然后,首先从Podfile目录运行pod update

  1. Swift Package Manager

创建一个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文件。