RxSwiftExtensions 1.2.3

RxSwiftExtensions 1.2.3

tokijh 维护。



 
依赖
RxSwift~> 4.3
RxCocoa~> 4.3
RxOptional~> 3.5
RxViewController>= 0
 

  • 作者:
  • tokijh

RxSwiftExtensions

Swift 4.2 Version License Platform

RxSwift 中常用的扩展


RxSwift 爱好者

这个库是根据 KakaoTalk 开放聊天室 中的问题构建的。
如果您对 RxSwift 有任何问题或感兴趣,请加入我们。我们总是欢迎😀
然而,这个聊天室在韩国运营。因此,您需要会说韩语🇰🇷


这个库是基于 카카오톡 오픈 채팅방 中提出的问题构建的。
如果您对 RxSwift 有任何问题或感兴趣,请随时轻松参与。😀
这个房间是在韩国运营的,可能需要韩语...(如果您能读到这一步,那么就没事了😂)


这个库是基于 カカオトークオープンチャットルーム 中提出的质問に基づいて作られました。
如果您对 RxSwift 有任何问题或兴趣,请随时轻松参与。😀
此房间由韩国运营,因此可能需要韩语。🇰🇷


功能

Swift
UIKit
RxSwift
RxCocoa

函数详情

Swift
  • KVO
    • 观察(keyPath: KeyPath) -> Observable

      通过键路径观察 KVO

      KVO 是 Objective-C 的机制。这意味着它并不是为了类型安全而构建的。这个项目试图解决一些问题。

      UIView()
        .observe(\.bounds, , options: [.initial, .new], retainSelf: true)
        .subscribe({ print($0) })
      
    • 观察弱引用(keyPath: KeyPath) -> Observable

      通过键路径观察 KVO

      rx.observeWeakly 比较慢的原因在于它需要处理弱引用下对象的释放。

      UIView()
        .observeWeakly(\.bounds, , options: [.initial, .new], retainSelf: true)
        .subscribe({ print($0) })
      
  • SectionDataType

    在 tableView 或 collectionView 中轻松定义分节数据

    定义跟进

    enum TestSectionData {
        case section1
        case section2([Value])
    }
    
    extension TestSectionData: SectionDataType {
    
        typealias Item = Value
    
        var items: [TestSectionData.Value] {
            switch self {
            case .section1: return [.string("Section 1")]
            case let .section2(value): return value
            }
        }
    
        enum Value {
            case string(String)
            case int(Int)
        }
    }
    

    你可以用它来跟进

    let section = sections[indexPath.section].items[indexPath.row]
    
    switch section {
      case let .int(value):
        cell.set(value)
        return cell
      case let .string(value):
        cell.set(value)
        return cell
    }
    
UIKit
  • UIView
    • UIView.rx.bounds

      观察 UIView 的 bounds

      UIView().rx.bounds.subscribe({ print($0) })
      
    • UIView.rx.center

      观察 UIView 的中心

      UIView().rx.center.subscribe({ print($0) })
      
  • UILabel
    • UILabel.rx.textColor

      Binder textColor 在 UILabel

      Observable<UIColor?>.just(UIColor.white).bind(to: UILabel().rx.textColor)
      
  • UITextView
    • UITextView.rx.textColor

      Binder textColor 在 UITextView

      Observable<UIColor?>.just(UIColor.white).bind(to: UITextView().rx.textColor)
      
  • UIScrollView
    • UIScrollView.rx.contentSize

      观察 UIScrollView 中的 contentSize

      UIScrollView().rx.contentSize.subscribe({ print($0) })
      
    • UIScrollView.rx.scrollableVertical

      观察垂直可滚动(bounds.height < contentSize.height)中的 UIScrollView

      UIScrollView().rx.scrollableVertical.subscribe({ print($0) })
      
    • UIScrollView.rx.scrollableHorizontal

      观察水平可滚动(bounds.width < contentSize.width)中的 UIScrollView

      UIScrollView().rx.scrollableHorizontal.subscribe({ print($0) })
      
  • UITableView
    • UITableView.register(cell: UITableViewCell.self)

      轻松注册 UITableViewCell

      以下示例将使用 "Cell"(《UITableViewCell_identifier》)进行注册。

      UITableView().register(cell: Cell.self)
      

      你还可以按照以下方式定义 Identifier

      UITableView().register(cell: Cell.self, forCellReuseIdentifier: "CustomIdentifier")
      
    • UITableView.register(nibCell: UITableViewCell.self)

      轻松使用 NIB 注册 UITableViewCell

      以下示例将使用 "Cell"(《UITableViewCell_identifier》)进行注册。

      UITableView().register(nibCell: Cell.self)
      

      你还可以按照以下方式定义 Identifier

      UITableView().register(nibCell: Cell.self, forCellReuseIdentifier: "CustomIdentifier")
      
    • UITableView.register(cell: UITableViewHeaderFooterView.self)

      轻松注册 UITableViewHeaderFooterView

      以下示例将使用 "Cell"(《UITableViewHeaderFooterView_identifier》)进行注册。

      UITableView().register(cell: Cell.self)
      

      你还可以按照以下方式定义 Identifier

      UITableView().register(cell: Cell.self, forCellReuseIdentifier: "CustomIdentifier")
      
    • UITableView.register(cell: UITableViewHeaderFooterView.self)

      轻松使用 NIB 注册 UITableViewCell

      以下示例将使用 "Cell"(《UITableViewHeaderFooterView_identifier》)进行注册。

      UITableView().register(cell: Cell.self)
      

      你还可以按照以下方式定义 Identifier

      UITableView().register(cell: Cell.self, forCellReuseIdentifier: "CustomIdentifier")
      
    • UITableView.dequeue(UITableViewCell.self)

      轻松 dequeue UICollectionViewCell

      以下示例将使用 "Cell" (《UICollectionReusableView_identifier》)进行 dequeue。

      let cell: Cell? = UITableView().dequeue(Cell.self)
      
    • UITableView.dequeue(UITableViewCell.self, indexPath: IndexPath)

      轻松 dequeue UICollectionViewCell

      以下示例将使用 "Cell" (《UICollectionReusableView_identifier》)进行 dequeue。

      let cell: Cell? = UITableView().dequeue(Cell.self, indexPath: IndexPath)
      
    • UITableView.dequeue(UITableViewHeaderFooterView.self)

      轻松 dequeue UITableViewHeaderFooterView

      let cell: Cell? = UITableView().dequeue(Cell.self)
      
    • UITableView.rx.items(Cell.self)

      通过 rx 绑定项

      Observable.just(["0", "1", "2", "3"])
          .bind(to: tableView.rx.items(cell: Cell.self)) { row, item, cell in
      
          }
      
  • UITableViewCell
    • UITableViewCell_identifier

      通过类名定义 Identifier

      class MyCell: UITableViewCell {
      
      }
      
      MyCell.Identifier == "MyCell"
      
  • UITableViewHeaderFooterView
    • UITableViewHeaderFooterView_identifier

      通过类名定义 Identifier

      class MyCell: UITableViewHeaderFooterView {
      
      }
      
      MyCell.Identifier == "MyCell"
      
  • UICollectionView
    • UICollectionView.register(cell: UICollectionViewCell.self)

      轻松注册 UICollectionViewCell

      以下示例将使用 "Cell"(《UICollectionReusableView_identifier》)进行注册。

      UICollectionView().register(cell: Cell.self)
      

      你还可以按照以下方式定义 Identifier

      UICollectionView().register(cell: Cell.self, forCellWithReuseIdentifier: "CustomIdentifier")
      
    • UICollectionView.register(nibCell: UICollectionViewCell.self)

      轻松使用 NIB 注册 UICollectionViewCell

      以下示例将使用 "Cell"(《UICollectionReusableView_identifier》)进行注册。

      UICollectionView().register(nibCell: Cell.self)
      

      你还可以按照以下方式定义 Identifier

      UICollectionView().register(nibCell: Cell.self, forCellWithReuseIdentifier: "CustomIdentifier")
      
    • UICollectionView.register(cell: UICollectionReusableView.self, forSupplementaryViewOfKind: SupplementaryViewOfKind)

      为SupplementaryViewOfKind注册UICollectionViewReusableView

      UICollectionView().register(cell: Cell.self, forSupplementaryViewOfKind: .header)
      
      UICollectionView().register(nibCell: Cell.self, forSupplementaryViewOfKind: .footer)
      
    • UICollectionView.dequeue(UICollectionViewCell.self, for: IndexPath)

      轻松 dequeue UICollectionViewCell

      以下示例将使用 "Cell" (《UICollectionReusableView_identifier》)进行 dequeue。

      let cell: Cell? = UICollectionView.dequeue(Cell.self, for: IndexPath)
      
    • UICollectionView.dequeue(UICollectionReusableView.self, ofKind: SupplementaryViewOfKind, for: IndexPath)

      轻松DeDequeue SupplementaryViewOfKind的UICollectionViewReusableView

      let cell: Cell? = UICollectionView.dequeue(Cell.self, ofKind: .header, for: IndexPath)
      
    • UICollectionView.rx.items(Cell.self)

      通过 rx 绑定项

      Observable.just(["0", "1", "2", "3"])
          .bind(to: collectionView.rx.items(cell: Cell.self)) { row, item, cell in
      
          }
      
  • UICollectionReusableView
    • UICollectionReusableView_identifier

      通过类名定义 Identifier

      class MyCell: UICollectionReusableView {
      
      }
      
      MyCell.Identifier == "MyCell"
      
  • UIViewController
    • UIViewController.rx.viewDidLoad观察当在UIViewController中调用`viewDidLoad`时
      UIViewController().rx.viewDidLoad.subscribe({ print($0) })
      
    • UIViewController.rx.viewWillAppear观察当在UIViewController中调用`viewWillAppear`时
      UIViewController().rx.viewWillAppear.subscribe({ print($0) })
      
    • UIViewController.rx.viewDidAppear观察当在UIViewController中调用`viewDidAppear`时
      UIViewController().rx.viewDidAppear.subscribe({ print($0) })
      
    • UIViewController.rx.viewWillDisappear观察当在UIViewController中调用`viewWillDisappear`时
      UIViewController().rx.viewWillDisappear.subscribe({ print($0) })
      
    • UIViewController.rx.viewDidDisappear观察当在UIViewController中调用`viewDidDisappear`时
      UIViewController().rx.viewDidDisappear.subscribe({ print($0) })
      
    • UIViewController.rx.viewWillLayoutSubviews观察当在UIViewController中调用`viewWillLayoutSubviews`时
      UIViewController().rx.viewWillLayoutSubviews.subscribe({ print($0) })
      
    • UIViewController.rx.viewDidLayoutSubviews观察当在UIViewController中调用`viewDidLayoutSubviews`时
      UIViewController().rx.viewDidLayoutSubviews.subscribe({ print($0) })
      
    • pictureBoxController.rx.willMove(toParentViewController: UIViewController)观察当在UIViewController中调用`willMove(toParentViewController: UIViewController)`时
      UIViewController().rx.willMove(toParentViewController: parentViewController).subscribe({ print($0) })
      
    • pictureBoxController.rx.didMove(toParentViewController: UIViewController)观察当在UIViewController中调用`didMove(toParentViewController: UIViewController)`时
      UIViewController().rx.didMove(toParentViewController: parentViewController).subscribe({ print($0) })
      
    • pictureBoxController.rx.didReceiveMemoryWarning观察当在UIViewController中调用`didReceiveMemoryWarning`时
      UIViewController().rx.didReceiveMemoryWarning.subscribe({ print($0) })
      
    • pictureBoxController.rx.isVisible触发时,ViewController的出现状态发生变化(如果视图正在显示,则为true,否则为false)
      UIViewController().rx.isVisible.subscribe({ print($0) })
      
    • pictureBoxController.rx.isDismissing当ViewController正在关闭时触发
      UIViewController().rx.isDismissing.subscribe({ print($0) })
      
RxSwift
  • ObservableType
    • withLatestFrom

      扩展withLatestFrom以支持多参数

      let a = Observable.just("A")
      let b = Observable.just("B")
      let c = Observable.just("C")
      
      Observable().withLatestFrom(a, b, c)
          .subscribe(onNext: { (a, b, c) in
              ...
          })
      
    • withLatestFromAndSelf

      扩展withLatestFromAndSelf以将参数与self结合

      let a = Observable.just("A")
      let b = Observable.just("B")
      let c = Observable.just("C")
      
      Observable().withLatestFromAndSelf(a, b, c)
          .subscribe(onNext: { (a, b, c) in
              ...
          })
      
    • bind(to [ObserverType]?)

      可以在一行中绑定多个ObserverType

      let publisher = PublishSubject<String>()
      
      let observers: [PublishSubject<String>] = [...]
      
      let disposable = publisher.bind(to: observers)
      
    • bind(to ObserverType?)

      如果Observer是nil,则可以绑定可选类型,它将忽略订阅

      let publisher = PublishSubject<String>()
      
      let observer: PublishSubject<String>? = nil
      
      let disposable = publisher.bind(to: observer)
      
RxCocoa
  • SharedSequenceConvertibleType
    • withLatestFrom

      扩展withLatestFrom以支持多参数

      let a = SharedSequenceConvertibleType.just("A")
      let b = SharedSequenceConvertibleType.just("B")
      let c = SharedSequenceConvertibleType.just("C")
      
      SharedSequenceConvertibleType().withLatestFrom(a, b, c)
          .subscribe(onNext: { (a, b, c) in
              ...
          })
      
    • withLatestFromAndSelf

      扩展withLatestFromAndSelf以将参数与self结合

      let a = SharedSequenceConvertibleType.just("A")
      let b = SharedSequenceConvertibleType.just("B")
      let c = SharedSequenceConvertibleType.just("C")
      
      SharedSequenceConvertibleType().withLatestFromAndSelf(a, b, c)
          .subscribe(onNext: { (a, b, c) in
              ...
          })
      

安装


使用的pods

作者

tokijh

许可证

RxSwiftExtensions可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。