Skip to content
Simple data binding operators ~> and <~> for RxSwift.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Example Update RxSwift to 5.0 May 21, 2019
RxBinding Fix the warnings for RxSwift 5.0 May 21, 2019
.gitignore Update README.md Apr 7, 2019
.swift-version Create .swift-version Apr 12, 2019
.travis.yml Update .travis.yml Apr 12, 2019
LICENSE Initial commit Mar 14, 2019
README.md Update RxSwift to 5.0 May 21, 2019
RxBinding.podspec Fix the warnings for RxSwift 5.0 May 21, 2019
_Pods.xcodeproj Initial commit Mar 14, 2019

README.md

RxBinding

CI Status Version License Platform swift

RxBinding provides ~>, <~> and ~ operators for data binding using RxSwift, to replace the bind(to:) and disposed(by:) method in RxSwift.

RxBinding is inspired by the following operators.

Documentation

RxBinding is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'RxBinding'

If you want to use the operators in the all files of your project, just import with @_expoerted.

@_exported import RxBinding

Usage of ~>

The type of text is Observable<String?> and the type of label.rx.text is Binder<String?>. RxSwfit provides the following method for the one way data binding between them.

viewModel.text.bind(to: label.rx.text).disposed(by: disposeBag)

With the ~> (bind(to:)) and ~ (disposed(by:)) in RxBinding, we can do the same thing with the following simple code.

viewModel.text ~> label.rx.text ~ disposeBag

Bind an observable object to multiple binders.

viewModel.text ~> [label1, label2].map { go0.rx.text } ~ disposeBag

Usage of <~>

The type of text is BehaviorRelay<String?> and the type of textFeild.rx.text is ControlProperty<String?>. To apply the two way data binding between them, we need the following code by RxSwift.

viewModel.text.bind(to: textFeild.rx.text).disposed(by: disposeBag)
textFeild.rx.text.bind(to: viewModel.text).disposed(by: disposeBag)

With the <~>, a simple two way bind operator, and ~ (disposed(by:)) in RxBinding, we can do the same thing with the following simple code.

viewModel.text <~> textFeild.rx.text ~ disposeBag

Multiple Bindings

RxBinding supports using a single disposeBag for multiple binding operators like this:

disposeBag ~ [
    viewModel.text <~> textFeild.rx.text,
    viewModel.uppercaseText ~> label.rx.text,
    viewModel.charactersCount ~> [characterCountLabel1, characterCountLabel2].map { $0.rx.text }
]

or this:

viewModel.text <~> textFeild.rx.text ~
viewModel.uppercaseText ~> label.rx.text ~
viewModel.charactersCount ~> [characterCountLabel1, characterCountLabel2].map { $0.rx.text }
  ~ disposeBag

NEED YOUR HELP

I am considering how to remove the operator ~ after the Binder or the ControlEvent property.

viewModel.text ~> label.rx.text

If anyone has a good idea about this, please contact me here https://github.com/RxSwiftCommunity/RxBinding/issues/1 or create a PR. Thanks.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Author

lm2343635, lm2343635@126.com

License

RxBinding is available under the MIT license. See the LICENSE file for more info.

You can’t perform that action at this time.