4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Swift】RxSwift勉強してみたPart7

Last updated at Posted at 2021-04-11

#はじめに
前回
今回はOptionalな値が流れるストリームに対してさまざまなことをできるようにする拡張ライブラリーであるRxOptionalをみていこうかと思います。

#RxOptional

###filterNil()
nilをはじいてくれるオペレーター

Observable<String?>
    .of("100", nil, "200")
    .filterNil() //Observable<String?> -> Observable<String>
    .subscribe { print($0) }
//100
//200

###replaceNilWith()
nilを置き換えることができるオペレーター

Observable<String?>
    .of("100", nil, "200")
    .replaceNilWith("300")
    .subscribe { print($0) }
//100
//300
//200

###errorOnNil()
nilが検出されたときにエラーをながすオペレーター

Observable<String?>
    .of("100", nil, "200")
    .errorOnNil()
    .subscribe { print($0) }
//100
//Found nil while trying to unwrap type<Optional<String>>

###filterEmpty()
空の配列や辞書をはじいてくれるオペレーター

Observable<[String]>
    .of(["1"], [], ["2", "3"])
    .filterEmpty()
    .subscribe { print($0) }
//["1"]
//["2", "3"]

#おわりに
次回
RxSwiftって拡張ライブラリーたくさんありますね!

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?