Last active
November 29, 2015 15:33
-
-
Save katryo/8d6a123209298b1ec880 to your computer and use it in GitHub Desktop.
お手製Swift Bondでリアクティブプログラミング ref: http://qiita.com/katryo/items/4f2473b72a629bb4257a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BondBox<T> { | |
weak var bond: Bond<T>? | |
init(_ b:Bond<T>) { | |
bond = b | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bondBoxes = bondBoxes.filter( | |
{ | |
(bondBox: BondBox<T>) -> Bool in | |
bondBox.bond != nil | |
} | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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