LoginSignup
2
2

More than 3 years have passed since last update.

[RxSwift]combineLatestについて

Posted at

Rxの学習過程で学んだことを後からでも見れるようにメモしていきます
本記事はこちらRxSwiftを参考にしています

CombineLatestとは

Observableシーケンスのいずれかが要素を生成するたびに、指定されたObservableシーケンスを、1つのタプルのObservableシーケンスにマージする。

使用例

TextFieldに入力した値をリアルタイムで合計し、ラベルに表示する。
といった機能を作っていきます。

数値入力用のTextFieldを3つと合計結果表示用のLabelをひとつ用意しいます。


Observable.combineLatest(textField1.rx.text.orEmpty, textField2.rx.text.orEmpty, textField3.rx.text.orEmpty) { textValue1, textValue2, textValue3 -> Int in
            return (Int(textValue1) ?? 0) + (Int(textValue2) ?? 0) + (Int(textValue3) ?? 0)
        }
        .map { $0.description }
        .bind(to: label.rx.text)
        .disposed(by: disposeBag)

結果

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2