Wednesday, October 7, 2026
HomeiOS Developmentios - Two-way connection between @Revealed (UIKit, Mix)

ios – Two-way connection between @Revealed (UIKit, Mix)

[ad_1]

I’ve a mannequin (Kind) and a button (CheckboxButton) each of them have @Revealed. I wish to have two-way connection (isOn <-> isChecked).
I am utilizing assign for connection and removeDuplicates() to keep away from cycle and it really works however seems very tough and I am undecided that it the most effective resolution.

Might you please recommend any concepts how can I enhance the code (two-way connection)?

class Kind: Codable {
    @Revealed var isOn: Bool = true {
        didSet {print(isOn)}
    }
}

class CheckboxButton: UIButton {
    @Revealed var isChecked: Bool = false  {
        didSet {setupImage()}
    }

    @objc func buttonDidSelect() {
        isChecked = !isChecked
    }
}

class Binding<Root, Worth> {
    let printed: Revealed<Worth>.Writer
    let keyPath: ReferenceWritableKeyPath<Root, Worth>
    let object: Root

    init(printed: Revealed<Worth>.Writer, to keyPath: ReferenceWritableKeyPath<Root, Worth>, on object: Root) {
        self.printed = printed
        self.keyPath = keyPath
        self.object = object
    }
}

addCheckboxButton( Binding(printed: type.$isOn, to: .isOn, on: self.type))
    
func addCheckboxButton(_ binding:  Binding<Kind,Bool>) {
     let button = CheckboxButton(title: "Checkbox Button", borders: false)
    
     button.$isChecked.removeDuplicates().assign(to: binding.keyPath, on: binding.object).retailer(in: &cancellables)
    
     binding.printed.removeDuplicates().assign(to: .isChecked, on: button).retailer(in: &cancellables)
}

P.S. On a display I’ve many buttons and every button has personal property within the mannequin (Kind). I’ve Bidding class to have code reuse chance.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments