본문 바로가기

iOS/Combine

Zip

Combines elements from two other publishers and delivers groups of elements as tuples.

  • 다른 두 퍼블리셔로 부터 값을 묶어서 튜플형태로 방출한다(단, zip은 각 퍼블리셔들의 값이 동일할 때만 묶어 방출한다)
zip.sink(receiveValue: { result in
    print("zip: \\(result.0), \\(result.1)")
})
.store(in: &cancellables)

left.send("Left")
right.send("Right")
right.send("Right2")
left.send("Left2")

// 결과값

zip: Left, Right
zip: Left2, Right2
  • combineLatest와 마찬가지로 처음에는 두 퍼블리셔에 값이 들어와야 동작하며, 두 퍼블리셔로 들어온 값을 튜플로 방출한다.
  • 단, combineLatest와는 다르게 한쪽 퍼블리셔만 값이 들어오는 경우에는 동작하지 않고 두 퍼블리셔에 수가 맞게 들어와야 묶어서 값을 방출한다.

 

예제코드

https://github.com/davidyoon891122/CombineProject

참조

https://reactivex.io/documentation/operators/zip.html

'iOS > Combine' 카테고리의 다른 글

CombineLatest  (0) 2024.04.23
WithLatestFrom  (0) 2024.04.22