I have three btns, select one, and unselect the rest two.
The following RXSwift code is not very elegant.
twoBtn.isSelected = true
// the btns
let buttons = [oneBtn, twoBtn, threeBtn]
// find the selected btn we need
let selectedBtn : Observable<UIButton?> = Observable.from(buttons.map({ (button) in
return button!.rx.tap.map({
return button
})
})).merge()
// every btn subscribe to the selectedButton
for buttonPiece in buttons{
selectedBtn.map { (btn) in
return btn == buttonPiece
}.bind(to: buttonPiece!.rx.isSelected).disposed(by: disposeBag)
}
The BehaviorSubject starts with an initial value, and they will replay the latest value or the initial value.
I think BehaviorSubject is proper here.
new to RxSwift, How to do it better?