'Swift Combine: is it available to obeserve zip multiple UIbuttons status?
Solution 1:[1]
I would solve the problem using combineLatest instead of zip:
import Cocoa
import Combine
let description1 = CurrentValueSubject<Bool, Never>(false)
let description2 = CurrentValueSubject<Bool, Never>(false)
let description3 = CurrentValueSubject<Bool, Never>(false)
let proceedEnable = description1.combineLatest(description2, description3)
.map { (triplet: (Bool, Bool, Bool)) in triplet.0 && triplet.1 && triplet.2 }
let subscription = proceedEnable.sink { print($0) }
description1.send(true)
description2.send(true)
description3.send(true)
description3.send(false)
You can copy and paste this code into a playground.
The CurrentValueSubject represents the state of the checkboxes.
The proceedEnable is a stream for the enabled state of the "Proceed" button. It will emit a value any time any of the description streams change and will only emit true if the three descriptionX values are all true.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Scott Thompson |

