Post

map 연산자, flatmap 연산자

map 연산자, flatmap 연산자

1. map 연산자

예제 코드

1
2
3
4
Observable.of(1, 2, 3)
    .map { $0 * $0 }
    .subscribe(onNext: { print($0) })
// 출력: 1, 4, 9

결과값

1
2
3
map result: RxSwift.Observable<Swift.Int>
map result: RxSwift.Observable<Swift.Int>
map result: RxSwift.Observable<Swift.Int>

2. flatMap 연산자:

예제 코드

1
2
3
4
5
6
7
8
Observable.of(1, 2, 3)
    .flatMap { num in 
        Observable.of(num, num * 10)
    }
    .subscribe(onNext: { value in
        print("flatMap result: \(value)")
    })
    .disposed(by: disposeBag)

결과값

1
2
3
4
5
6
flatMap result: 1
flatMap result: 10
flatMap result: 2
flatMap result: 20
flatMap result: 3
flatMap result: 30
This post is licensed under CC BY 4.0 by the author.