Search

map

태그
Combine 카테고리
오퍼레이터
사용용도 요약
- 발행된 값을 변형하여 재발행
세부 카테고리
작성일
2022/07/13
카테고리
Combine

map

upstream 에서 발행된 값을 변형해서 재 발행한다.
//... .map { formatter.string(for: NSNumber(integerLiteral: $0)) ?? "" } //...
Swift
복사

key path 를 활용한 mapping

map<T>(:)
map<T0, T1>(::)
map<T0, T1, T2>(:::)
key path 를 활용해, 발행될 타입의 프로퍼티로 mapping → 프로퍼티의 타입으로 값을 재 발행한다.
example(of: "mapping key paths") { let publisher = PassthroughSubject<Coordinate, Never>() publisher .map(\.x, \.y) .sink(receiveValue: { x, y in print( "The coordinate at (\(x), \(y)) is in quadrant", quadrantOf(x: x, y: y) ) }) .store(in: &subscriptions) publisher.send(Coordinate(x: 10, y: -8)) publisher.send(Coordinate(x: 0, y: 5)) }
Swift
복사

tryMap

실패할 수 있는 변환 작업을 수행해 값을 재발행한다.
throwing 메서드 즉, 에러를 발생할 수 있는 메서드를 호출할 수 있게 된다.
→ 발생된 에러는 completion failure 이벤트로 발행된다.
example(of: "tryMap") { Just("Directory name that does not exist") .tryMap { try FileManager.default.contentsOfDirectory(atPath: $0) } .sink(receiveCompletion: { print($0) }, receiveValue: { print($0) }) .store(in: &subscriptions) } ——— Example of: tryMap ——— failure(..."The folder “Directory name that does not exist” doesn't exist."...)
Swift
복사

flatMap(maxPublishers:_:)

여러개의 upstream 퍼블리셔를 하나의 downstream publisher 로 만들어준다.
먼저
Swift
복사