{"id":2685,"date":"2018-08-09T15:09:20","date_gmt":"2018-08-09T06:09:20","guid":{"rendered":"http:\/\/www.whoocus.com\/blog\/?p=2685"},"modified":"2018-08-09T15:11:21","modified_gmt":"2018-08-09T06:11:21","slug":"rxswift%e3%81%ae%e3%82%b7%e3%83%b3%e3%83%97%e3%83%ab%e3%81%aa%e8%aa%ac%e6%98%8e","status":"publish","type":"post","link":"https:\/\/www.blowfish.co.jp\/?p=2685","title":{"rendered":"RxSwift\u306e\u30b7\u30f3\u30d7\u30eb\u306a\u8aac\u660e"},"content":{"rendered":"<p>RxSwift\u306e\u30b7\u30f3\u30d7\u30eb\u306a\u8aac\u660e<\/p>\n<p>Swift\u3067\u7c21\u5358\u306a\u30a2\u30d7\u30ea\u3092\u5199\u7d4c\u3059\u308b\u306e\u306f\u7c21\u5358\u306a\u306e\u3067\u3042\u308b\u304c\u3001\u3084\u306f\u308a\u5b9f\u7528\u7684\u306a\u30a2\u30d7\u30ea\u3092\u4f5c\u6210\u3059\u308b\u306b\u306f\u3001\u30ec\u30b9\u30dd\u30f3\u30b7\u30d6\u3067\u306a\u3044\u3068\u5f79\u306a\u7acb\u305f\u306a\u3044(\u304a\u5ba2\u3055\u3093\u53d7\u3051\u3057\u306a\u3044)\u3088\u306d\u3068\u3044\u3046\u3053\u3068\u3067\u3001<code>RxSwift<\/code>\u3092\u5b66\u3093\u3067\u307f\u305f\u3002<\/p>\n<p>\u6b63\u76f4\u30013\u65e5\u9593\u304f\u3089\u3044\u3001\u6ce3\u304d\u306a\u304c\u3089\u3001\u632b\u3051\u305d\u3046\u306b\u306a\u308a\u306a\u304c\u3089\u3001\u79bf\u3052\u305d\u3046\u306b\u306a\u308a\u306a\u304c\u3089\u98df\u3089\u3044\u3064\u3044\u3066\u3044\u305f\u3002<\/p>\n<p>\u306f\u3063\u304d\u308a\u3044\u3063\u3066\u7406\u89e3\u3059\u308b\u306e\u304c\u30ad\u30c4\u30a4\u3002<\/p>\n<p>\u25a0 \u958b\u767a\u74b0\u5883<br \/>\nOS : MacOS High Sierra Version 10.13.6<br \/>\nXcode : Version 9.4.1<\/p>\n<p>\u25a0 \u30b5\u30f3\u30d7\u30ea\u30a2\u30d7\u30ea<br \/>\n\u30dc\u30bf\u30f3\u3092\u62bc\u3059\u3068\u30ab\u30a6\u30f3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u3060\u3051\u306e\u30a2\u30d7\u30ea\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/qiita-image-store.s3.amazonaws.com\/0\/3184\/7b9dac80-cd38-6546-bd3e-5a9b5fbf7e41.png\" alt=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2018-08-09 14.11.12.png\" title=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2018-08-09 14.11.12.png\" \/><\/p>\n<p>\u25a0 \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u89e3\u8aac<br \/>\n<code>ViewController.swift<\/code>\u306e\u30b3\u30fc\u30c9\u3092\u8aac\u660e\u3059\u308b\u3002<\/p>\n<p>\u307e\u305a\u306f\u3001\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u3092import\u3059\u308b\u3002\u3053\u3053\u3067\u306f\u3001<code>RxSwift<\/code>\u3092\u5229\u7528\u3059\u308b\u3002<\/p>\n<p>[objc]<br \/>\nimport UIKit<br \/>\nimport RxSwift<br \/>\n[\/objc]<\/p>\n<p>\u5148\u306b\u3001\u30e9\u30d9\u30eb\u3068\u30dc\u30bf\u30f3\u3092\u914d\u7f6e\u3059\u308b\u3002<br \/>\nStreatBoard\u3067\u3082\u826f\u3044\u306e\u3067\u3042\u308b\u304c\u3001\u6562\u3048\u3066\u30b3\u30fc\u30c9\u3067\u8a18\u8ff0\u3002<\/p>\n<p>[objc]<br \/>\nclass ViewController: UIViewController {<\/p>\n<p>    override func viewDidLoad() {<br \/>\n        super.viewDidLoad()<br \/>\n        \/\/ Label<br \/>\n        let countLbl = UILabel()<br \/>\n        countLbl.text = &quot;0&quot;<br \/>\n        countLbl.textAlignment = .center<br \/>\n        countLbl.textColor = UIColor.blue<br \/>\n        countLbl.frame = CGRect(x: (self.view.frame.width-110)\/2, y: 150, width: 110, height: 21)<\/p>\n<p>        \/\/ Button<br \/>\n        let countUpBtn = UIButton()<br \/>\n        countUpBtn.frame = CGRect(x: (self.view.frame.width-100)\/2, y: 200, width: 100, height: 30)<br \/>\n        countUpBtn.layer.borderWidth = 1.0<br \/>\n        countUpBtn.layer.borderColor = UIColor.blue.cgColor<br \/>\n        countUpBtn.layer.cornerRadius = 5.0<br \/>\n        countUpBtn.setTitleColor(UIColor.blue, for: .normal)<br \/>\n        countUpBtn.setTitle(&quot;\u30ab\u30a6\u30f3\u30c8UP&quot;, for: .normal)<br \/>\n        countUpBtn.addTarget(self, action: #selector(ViewController.countUp(_:)), for: UIControlEvents.touchUpInside)<\/p>\n<p>        self.view.addSubview(countLbl)<br \/>\n        self.view.addSubview(countUpBtn)<br \/>\n}<br \/>\n[\/objc]<\/p>\n<p>\u6b21\u306b\u3001<code>RxSwift<\/code>\u306e\u809d\u3067\u3042\u308b\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3059\u308b\u3002<\/p>\n<p>[objc]<br \/>\nclass RxSwiftSample {<\/p>\n<p>}<br \/>\n[\/objc]<\/p>\n<p><code>RxSwiftSample<\/code>\u30af\u30e9\u30b9\u306b\u306f\u3001private\u5ba3\u8a00\u3055\u308c\u305f<code>subject<\/code>\u3092<code>PublishSubject&lt;Int&gt;()<\/code>\u3067\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3057\u3066\u4fdd\u6301\u3057\u3066\u304a\u304f\u3002\u305d\u308c\u3068\u3001\u305d\u306esubject\u3092Observable\u3068\u3057\u3066\u8fd4\u3059event\u3092\u5b9a\u7fa9\u3059\u308b\u3002<br \/>\n\u305d\u3057\u3066\u3001\u5b9f\u969b\u306e\u51e6\u7406\u3092\u5916\u90e8\u304b\u3089\u547c\u3079\u308b <code>myFunc()<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u5b9a\u7fa9\u3059\u308b\u3002\u305d\u306e\u4e2d\u3067\u3001<code>onNext<\/code>\u3068\u304b\u3044\u3046\u30a4\u30d9\u30f3\u30c8\u3092\u767a\u884c\u3059\u308b\u3002<\/p>\n<p>\u3053\u308c\u304c\u57fa\u672c\u7684\u306a\u96db\u5f62\u3068\u3044\u3046\u611f\u3058\u304b\u306a\uff1f<\/p>\n<p>[objc]<br \/>\nclass RxSwiftSample {<br \/>\n    private let subject = PublishSubject&lt;Int&gt;()<\/p>\n<p>    var event : Observable&lt;Int&gt;{<br \/>\n        return subject<br \/>\n    }<\/p>\n<p>    func myFunc(){<br \/>\n        \/\/ \u51e6\u7406<br \/>\n        print(&quot;myFunc\u304c\u547c\u3070\u308c\u307e\u3057\u305f\u3002&quot;)<br \/>\n        \/\/ \u30a4\u30d9\u30f3\u30c8\u767a\u884c<br \/>\n        subject.onNext(self.data)<br \/>\n    }<br \/>\n}<br \/>\n[\/objc]<\/p>\n<p>\u6b21\u306b\u3001\u3053\u306e<code>RxSwiftSample<\/code>\u30af\u30e9\u30b9\u306e\u4f7f\u3044\u65b9\u306a\u306e\u3067\u3042\u308b\u304c\u3001<code>ViewController<\/code>\u304b\u3089\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u3001<code>RxSwiftSample<\/code>\u3092\u30e2\u30c7\u30eb\u3068\u3057\u3066<code>model<\/code>\u3067\u5ba3\u8a00\u3057\u3066\u304a\u304f\u3002\u307e\u305f\u3001\u5f8c\u3067\u5229\u7528\u3059\u308b<code>disposeBag<\/code>\u3082\u5ba3\u8a00\u3057\u3066\u304a\u304f\u3002<\/p>\n<p>[objc]<br \/>\nprivate let model = RxSwiftSample()<br \/>\nprivate let disposeBag = DisposeBag()<br \/>\n[\/objc]<\/p>\n<p>\u4e0a\u8a18\u3092\u3001<code>ViewController<\/code>\u304b\u3089\u5229\u7528\u3059\u308b\u3002<br \/>\n\u5148\u305a\u306f\u3001\u9806\u3092\u8ffd\u3063\u3066\u308f\u304b\u308a\u3084\u3059\u304f\u3001\u30dc\u30bf\u30f3\u306b\u5bfe\u3057\u3066\u3001<code>countUp(_ sender: UIButton)<\/code>\u304c\u547c\u3070\u308c\u308b\u3088\u3046\u306b\u8a18\u8ff0\u3059\u308b\u3002\u3053\u308c\u306f\u7279\u306b\u554f\u984c\u306f\u306a\u3044\u306f\u305a\u3002<br \/>\n\u91cd\u8981\u306a\u306e\u306f\u3001\u3053\u3053\u304b\u3089<code>model<\/code>\u306e<code>myFunc()<\/code>\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u3067\u3042\u308b\u3002<\/p>\n<p>[objc]<br \/>\n    override func viewDidLoad() {<br \/>\n     \/\/\u30dc\u30bf\u30f3\u3078\u30a4\u30d9\u30f3\u30c8\u767b\u9332<br \/>\n        countUpBtn.addTarget(self, action: #selector(ViewController.countUp(_:)), for: UIControlEvents.touchUpInside)<br \/>\n    }<\/p>\n<p>    @objc func countUp(_ sender: UIButton){<br \/>\n        print(&quot;\u30ab\u30a6\u30f3\u30c8\u30a2\u30c3\u30d7&quot;)<br \/>\n        \/\/Model\u306e\u95a2\u6570\u547c\u3073\u51fa\u3057<br \/>\n        model.myFunc()<br \/>\n    }<\/p>\n<p>[\/objc]<\/p>\n<p>\u3053\u3053\u304b\u3089\u3001\u7406\u89e3\u304c\u9032\u3080\u3068\u601d\u3046\u304c\u3001<code>model.myFunc()<\/code>\u3067\u51e6\u7406\u3092\u884c\u3046\u3068\u3001\u4e8b\u524d\u306b\u767b\u9332\u3055\u308c\u3066\u3044\u308b<code>subscribe<\/code>\u3067\u901a\u77e5\u3055\u308c\u308b\u3002\u305d\u3057\u3066\u3001 <code>subject.onNext(self.data)<\/code>\u3067\u6e21\u3055\u308c\u305f\u30d1\u30e9\u30e1\u30fc\u30bf\u3082\u3001value \u3067\u53d7\u3051\u53d6\u308c\u308b\u3002<\/p>\n<p>[objc]<br \/>\n        \/\/Reactive\u51e6\u7406<br \/>\n        model.event.subscribe(<br \/>\n                onNext: {value in<br \/>\n                    print(&quot;\u3053\u3053\u3067\u901a\u77e5&quot;)<br \/>\n                    countLbl.text = String(value)<br \/>\n                })<br \/>\n                .disposed(by: disposeBag)<\/p>\n<p>[\/objc]<\/p>\n<p>\u3053\u308c\u3067\u3001\u3088\u3046\u3084\u304f\u982d\u306e\u4e2d\u3067\u51e6\u7406\u30b7\u30fc\u30b1\u30f3\u30b9\u304c\u3064\u306a\u304c\u3063\u305f\u3002<br \/>\n\u305d\u308c\u3067\u3082\u7406\u89e3\u3067\u304d\u306a\u3044\u5834\u5408\u306f\u3001\u79c1\u3068\u540c\u3058\u3088\u3046\u306b1\u65e5\u60a9\u3093\u3067\u3001<code>print()<\/code>\u4ed5\u639b\u3051\u3066\u3001\u51e6\u7406\u3092\u8ffd\u3063\u3066\u307f\u308b\u3068\u826f\u3044\u3068\u601d\u3046\u3002<\/p>\n<p>\u3084\u306f\u308a\u3001\u3053\u306e\u3088\u3046\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3084\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u306e\u7406\u89e3\u306f\u6b63\u76f4\u30ad\u30c4\u30a4\u3002\u305d\u308c\u3067\u3082\u3001\u6642\u9593\u3092\u639b\u3051\u3066\u624b\u3092\u52d5\u304b\u3059\u306e\u304c\u3001\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u3060\u3068\u601d\u3046\u3002<\/p>\n<p>\u305d\u306e\u52a9\u3051\u306b\u306a\u308c\u3070\u3068\u7948\u308b\u3002\u60c5\u5831\u304c\u3061\u3087\u3063\u3068\u6642\u4ee3\u9045\u308c\u611f\u306f\u3042\u308b\u306e\u3060\u304c\u30fb\u30fb\u30fb\u3002<br \/>\n\u59cb\u3081\u305f\u306e\u304c\u9045\u3044\u306e\u3067\u4ed5\u65b9\u306a\u3044\u3002w<\/p>\n<p>\u25a0 \u6700\u5f8c\u306b<\/p>\n<p>\u3053\u3053\u306b\u8a18\u3057\u305f\u30b3\u30fc\u30c9\u306f\u3001\u81ea\u5206\u304c<code>RxSwift<\/code>\u306e\u69cb\u9020\u3092\u7406\u89e3\u3059\u308b\u305f\u3081\u306b\u4f5c\u6210\u3057\u305f\u30b3\u30fc\u30c9\u3067\u3059\u3002\u9593\u9055\u3044\u7b49\u304c\u3054\u3056\u3044\u307e\u3057\u305f\u3089\u3054\u6307\u6458\u9858\u3044\u307e\u3059\u3002<\/p>\n<p>\u4e0b\u8a18\u3001\u5168\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3067\u3059\u3002<\/p>\n<p>[objc]<br \/>\nimport UIKit<br \/>\nimport RxSwift<\/p>\n<p>private let model = RxSwiftSample()<br \/>\nprivate let disposeBag = DisposeBag()<\/p>\n<p>class ViewController: UIViewController {<\/p>\n<p>    override func viewDidLoad() {<br \/>\n        super.viewDidLoad()<br \/>\n        \/\/ Label<br \/>\n        let countLbl = UILabel()<br \/>\n        countLbl.text = &quot;0&quot;<br \/>\n        countLbl.textAlignment = .center<br \/>\n        countLbl.textColor = UIColor.blue<br \/>\n        countLbl.frame = CGRect(x: (self.view.frame.width-110)\/2, y: 150, width: 110, height: 21)<\/p>\n<p>        \/\/ Button<br \/>\n        let countUpBtn = UIButton()<br \/>\n        countUpBtn.frame = CGRect(x: (self.view.frame.width-100)\/2, y: 200, width: 100, height: 30)<br \/>\n        countUpBtn.layer.borderWidth = 1.0<br \/>\n        countUpBtn.layer.borderColor = UIColor.blue.cgColor<br \/>\n        countUpBtn.layer.cornerRadius = 5.0<br \/>\n        countUpBtn.setTitleColor(UIColor.blue, for: .normal)<br \/>\n        countUpBtn.setTitle(&quot;\u30ab\u30a6\u30f3\u30c8UP&quot;, for: .normal)<br \/>\n        countUpBtn.addTarget(self, action: #selector(ViewController.countUp(_:)), for: UIControlEvents.touchUpInside)<\/p>\n<p>        self.view.addSubview(countLbl)<br \/>\n        self.view.addSubview(countUpBtn)<\/p>\n<p>        \/\/Reactive\u51e6\u7406<br \/>\n        model.event.subscribe(<br \/>\n                onNext: {value in<br \/>\n                    countLbl.text = String(value)<br \/>\n                })<br \/>\n                .disposed(by: disposeBag)<br \/>\n    }<\/p>\n<p>    @objc func countUp(_ sender: UIButton){<br \/>\n        \/\/Model\u306e\u95a2\u6570\u547c\u3073\u51fa\u3057<br \/>\n        model.myFunc()<br \/>\n    }<\/p>\n<p>    override func didReceiveMemoryWarning() {<br \/>\n        super.didReceiveMemoryWarning()<br \/>\n    }<\/p>\n<p>}<\/p>\n<p>class RxSwiftSample {<br \/>\n    private let subject = PublishSubject&lt;Int&gt;()<br \/>\n    private var data = 0<\/p>\n<p>    var event : Observable&lt;Int&gt;{<br \/>\n        return subject<br \/>\n    }<\/p>\n<p>    func myFunc(){<br \/>\n        \/\/ \u51e6\u7406<br \/>\n        data = data + 1<\/p>\n<p>        \/\/ \u30a4\u30d9\u30f3\u30c8\u767a\u884c<br \/>\n        subject.onNext(self.data)<br \/>\n    }<br \/>\n}<br \/>\n[\/objc]<\/p>\n<p><em>Lovly Swift!!!<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>RxSwift\u306e\u30b7\u30f3\u30d7\u30eb\u306a\u8aac\u660e Swift\u3067\u7c21\u5358\u306a\u30a2\u30d7\u30ea\u3092\u5199\u7d4c\u3059\u308b\u306e\u306f\u7c21\u5358\u306a\u306e\u3067\u3042\u308b\u304c\u3001\u3084\u306f\u308a\u5b9f\u7528\u7684\u306a\u30a2\u30d7\u30ea\u3092\u4f5c\u6210\u3059\u308b\u306b\u306f\u3001\u30ec\u30b9\u30dd\u30f3\u30b7\u30d6\u3067\u306a\u3044\u3068\u5f79\u306a\u7acb\u305f\u306a\u3044(\u304a\u5ba2\u3055\u3093\u53d7\u3051\u3057\u306a\u3044)\u3088\u306d\u3068\u3044\u3046\u3053\u3068\u3067\u3001RxSwift\u3092\u5b66\u3093\u3067\u307f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"saved_in_kubio":false,"footnotes":""},"categories":[49,47,50,4],"tags":[],"class_list":["post-2685","post","type-post","status-publish","format-standard","hentry","category-ios","category-swift","category-xcode","category-tech"],"_links":{"self":[{"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/posts\/2685","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2685"}],"version-history":[{"count":2,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/posts\/2685\/revisions"}],"predecessor-version":[{"id":2687,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/posts\/2685\/revisions\/2687"}],"wp:attachment":[{"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}