Last active
May 30, 2016 05:11
-
-
Save shoheiyokoyama/6b95240e3ba193658e16192b3327b732 to your computer and use it in GitHub Desktop.
SwiftでReactive Programming ref: http://qiita.com/shoheiyokoyama/items/60aa617ab39778d73110
This file contains hidden or 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
| stream | |
| .buffer(関数) | |
| .map(関数) | |
| .filter(関数) | |
| .subscribe(処理) |
This file contains hidden or 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
| var c = "" | |
| var a = 1 | |
| var b = 2 | |
| if a + b >= 0 { | |
| c = "\(a + b) is positive" | |
| } | |
| a = 4 | |
| print(c)//"3 is positive" |
This file contains hidden or 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
| import RxSwift | |
| let a = Variable(1) | |
| let b = Variable(2) | |
| let c = Observable.combineLatest(a.asObservable(), b.asObservable()) { $0 + $1 } | |
| .filter { $0 >= 0 } | |
| .map { "\($0) is positive" } | |
| .subscribeNext { print($0) }//"3 is positive" | |
| a.value = 4//"6 is positive" | |
| b.value = 6//"10 is positive" | |
| b.value = -8// doesn't print anything |
This file contains hidden or 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
| let a = Variable(1) | |
| let b = Variable(2) |
This file contains hidden or 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
| let c = Observable.combineLatest(a.asObservable(), b.asObservable()) { $0 + $1 }//Observableを返している | |
| .filter { $0 >= 0 }//combineLatestを参照し、新しいObservableを返す | |
| .map { "\($0) is positive" }//filterを参照し、新しいObservableを返す | |
| .subscribeNext { print($0) }//mapを参照し、新しいObservableを返す |
This file contains hidden or 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
| import RxSwift | |
| import RxCocoa | |
| ... | |
| @IBOutlet weak var textField: UITextField! | |
| @IBOutlet weak var label: UILabel! | |
| let disposeBag = DisposeBag() | |
| ... | |
| textField.rx_text | |
| .subscribeNext { [unowned self] text in | |
| self.label.text = text | |
| } | |
| .addDisposableTo(disposeBag) |
This file contains hidden or 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
| import RxSwift | |
| import RxCocoa | |
| ... | |
| @IBOutlet weak var button: UIButton! | |
| let disposeBag = DisposeBag() | |
| ... | |
| button.rx_tap | |
| .subscribeNext { _ in | |
| print("tapped!") | |
| } | |
| .addDisposableTo(disposeBag) |
This file contains hidden or 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
| import RxSwift | |
| import RxCocoa | |
| ... | |
| @IBOutlet weak var slider: UISlider! | |
| @IBOutlet weak var label: UILabel! | |
| let disposeBag = DisposeBag() | |
| ... | |
| slider.rx_value | |
| .map { String($0) } | |
| .subscribeNext { [unowned self] text in | |
| self.label.text = text | |
| } | |
| .addDisposableTo(disposeBag) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment