Skip to content

Instantly share code, notes, and snippets.

@bricklife
Created August 25, 2017 10:39
Show Gist options
  • Save bricklife/a279e9e6049d9e3c297dce249e6ca020 to your computer and use it in GitHub Desktop.
Save bricklife/a279e9e6049d9e3c297dce249e6ca020 to your computer and use it in GitHub Desktop.
bind(to:) function for ReactiveSwift
import ReactiveSwift
import enum Result.NoError
extension BindingSource {
@discardableResult
func bind<P: BindingTargetProvider>(to provider: P) -> Disposable? where P.Value == Value, Error == NoError {
return provider <~ self
}
@discardableResult
func bind<P: BindingTargetProvider>(to provider: P) -> Disposable? where P.Value == Value?, Error == NoError {
return provider <~ self
}
}
let label = UILabel()
let textProperty = MutableProperty("")
// Not good for me...
let disposable = label.reactive.text <~ textProperty
// Good!
let disposable = textProperty.bind(to: label.reactive.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment