[![CI Status](http://img.shields.io/travis/Ryo Kato/MiniBond.svg?style=flat)](https://travis-ci.org/Ryo Kato/MiniBond)
MiniBond is a very simple tool for reactive progamming in Swift using observer pattern.
The basic idea is came from SwiftBond https://github.com/SwiftBond/Bond
- Create a
EventProducer
object with the argument that is the real object you want to observe the change. This argument is set to theEventProducer
'svalue
property. - Create an
Observable
object with a closure that is called when theEventProducer
'svalue
object changes. - Bind to the
EventProducer
with theObservable
object byObservable.bindTo(EventProducer)
. - Then, when
EventProducer
's value object changes, the closure is called.
Here is a example usage you can find in the exemple project.
EventProducer
object iscount
.Observable
object itcountObservable
.
class ViewController: UIViewController {
let count = EventProducer<Int>(0)
var countObservable: Observable<Int>?
@IBOutlet weak var countLabel: UILabel!
@IBAction func buttonPushed(sender: UIButton) {
self.count.value += 1
}
override func viewDidLoad() {
super.viewDidLoad()
countObservable = Observable<Int>{ [unowned self] eventProducer in
self.countLabel.text = String(self.count.value)
}
countObservable?.bindTo(count)
}
}
- Create
count
that is anEventProducer
with the argument0
. This integer is what you want to observe the change and set tocount.value
. In other words,count.value == 0
- Create
countObservable
with the closure that changes the label with the number ofself.count.value
. - Bind to the
count
with thecountObservable
bycountObservable.bindTo(count)
. - Then, when
EventProducer
'svalue
object changes, the closure is called.
In the example, when buttonPushed
is called, the number of self.count.value
is increased, so the closure
{ [unowned self] eventProducer in
self.countLabel.text = String(self.count.value)
}
is called.
[unowned self]
is recommended since it prevents memory leak.
To run the example project, clone the repo, and run pod install
from the Example directory first.
Swift 1.2+
MiniBond is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "MiniBond"
katryo
MiniBond is available under the MIT license. See the LICENSE file for more info.