Swift Combine: is it available to obeserve zip multiple UIbuttons status?
I would like to enable "Proceed" button when every checkbox buttons are selected using combine and publisher.
There must be the way to zip those multiple UIButtons(works on UIKit) isSelected status are all true.
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.