Unidirectional binding operators and reactive extensions for Cocoa classes.
Bindings provides two operators: <~, the input binding operator, and
~>, the output binding operator.
import Bindings
import UIKitBindings
nameLabel.reactive.text <~ viewModel.fullNamenameField.reactive.edited ~> viewModel.setName
UIApplication.reactive.didBecomeActiveNotification ~> viewModel.refreshIn addition to the core operator definitions, Bindings also provides conveniences for using bindings with many system classes. Take a look at the following modules to see what's provided (and consider [contributing][CONTRIBUTING]!):
UIKitBindingsfor apps using UIKit (iOS, iPadOS, Catalyst and more)- More to come...
It's convenient to group reactive extensions into their own namespace, especially when extending types you don't own with reactive APIs.
Use the ReactiveExtensionProvider protocol to define the .reactive property
on your types:
extension MyViewModel: ReactiveExtensionProvider {}Then add reactive extensions via the Reactive type:
extension UserDefaults {
var username: String? {
get { string(forKey: "username") }
set { set(newValue, forKey: "username") }
}
}
extension Reactive where Base: UserDefaults {
var username: NSObject.KeyValueObservingPublisher<Base, String?> {
base.publisher(for: \.username)
}
}
UserDefaults.standard.reactive.username.sink { username in
print("New username: \(username)")
}In Combine, the AnyCancellable class controls the lifetime of a subscription.
Subscriptions can be automatically cancelled by using the store(in:) method:
import UIKit
class MyViewController: UIViewController {
@IBOutlet private var usernameLabel: UILabel!
private var subscriptions: [AnyCancellable] = []
override func viewDidLoad() {
super.viewDidLoad()
UserDefaults.standard.reactive.username
.sink { [usernameLabel] in usernameLabel?.text = $0 }
.store(in: &subscriptions)
}
}Bindings encapsulates this pattern with the BindingOwner protocol, which
automatically defines a subscriptions collection on all conforming classes.
The BindingSink subscriber then automatically disposes of the binding
subscription when its owner goes out of scope:
extension Reactive where Base: UILabel {
var text: BindingSink<Base, String?> {
BindingSink(owner: base) { label, newValue in
label.text = newValue
}
}
}
// automatically cancelled when `usernameLabel` goes out of scope
usernameLabel.reactive.text <~ UserDefaults.standard.reactive.username