Observable is the easiest way to observe values in Swift.
var position = Observable(CGPoint.zero)
position.observe { p in
// handle new position
}
position.value = p
For a single observer you can store the returned Disposable
to a variable
disposable = position.observe { p in
For multiple observers you can add the disposable to a Disposal
variable
position.observe { }.add(to: &disposal)
And always weakify self
when referencing self
inside your observer
position.observe { [weak self] position in
Observable is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'Observable'
Observable is available through Carthage. To install it, simply add the following line to your Cartfile:
github "roberthein/Observable" "master"
Feel free to create a pull request, open an issue or find me on Twitter.