Predict the output
What does this print?
let a = PassthroughSubject<Int, Never>()
let b = PassthroughSubject<String, Never>()
a.combineLatest(b)
.sink { print($0, $1) }
.store(in: &cancellables)
a.send(1)
a.send(2)
b.send("x")
a.send(3)Hint 1 of 1
Nothing is emitted until both have sent at least once.
Choose an answer, then open Show answer below to check it.
Show answer
Answer: B 2 x, 3 x
Correct. The first output appears when b emits, using a's latest value, 2.
Why not the others?
- A 1 x, 2 x, 3 x. Values sent before b's first value aren't replayed; only the latest is kept.
- C 3 x. b's first value produces an output with a = 2 before a sends 3.
- D 1 x, 3 x. combineLatest keeps only the latest value of a, which is 2 when b emits.