Skip to content

Instantly share code, notes, and snippets.

@katryo
Last active November 29, 2015 15:33
Show Gist options
  • Save katryo/8d6a123209298b1ec880 to your computer and use it in GitHub Desktop.
Save katryo/8d6a123209298b1ec880 to your computer and use it in GitHub Desktop.
お手製Swift Bondでリアクティブプログラミング ref: http://qiita.com/katryo/items/4f2473b72a629bb4257a
class Bond<T> {
typealias Listener = T -> Void
var listener: Listener
init(_ listener: Listener) {
self.listener = listener
}
func bind(dynamic: Dynamic<T>) {
dynamic.bondBoxes.append(BondBox(self))
}
}
class BondBox<T> {
weak var bond: Bond<T>?
init(_ b:Bond<T>) {
bond = b
}
}
bondBoxes = bondBoxes.filter(
{
(bondBox: BondBox<T>) -> Bool in
bondBox.bond != nil
}
)
class MenuViewController: UITableViewController {
var story: Story?
var starCountBond: Bond<Int>?
override func viewDidLoad() {
super.viewDidLoad()
starCountBond = Bond<Int> { [unowned self] viewingCount in
self.tableView!.reloadData()
}
starCountBond!.bind(story!.starCount)
}
(略)
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
(略)
cell!.detailTextLabel!.text = String(story!.starCount.value)
class Story {
let title: String
let body: String
var starCount: Dynamic<Int>
init(title: String, body: String, starCount: Int = 0) {
self.title = title
self.body = body
self.starCount = Dynamic(starCount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment