Custom RxSwift Bindable Property

Lee young-jun
2 min readDec 6, 2023

--

RxSwift Logo with Circle Progress

I created custom progress view long time ago.

Since I decided to apply Rx to my app, I wanted to bind to custom component’s property like this

.drive(self.prog_target.rx.progress)

RxCocoa

However I didn’t know how can I make it. So I jumped to the built-in rx extension for UILabel.

self.lb_nameValue.rx.text

And I found how the extension is implemented, it was very simple and contained in RxCocoa.

extension Reactive where Base: UILabel {

/// Bindable sink for `text` property.
public var text: Binder<String?> {
return Binder(self.base) { label, text in
label.text = text
}
}

Extension

Therefore I created my own extension.

extension Reactive where Base: LSCircleProgressView {
/// Bindable sink for `progress` property.
var progress: Binder<Float> {
return Binder(self.base) { control, value in
control.progress = value;
}
}
}

Advanced

I also look into Reactive and I could see that the Base is generic argument.

public struct Reactive<Base> {
/// Base object to extend.
public let base: Base

So I guessed where Base: …can be removed and the extension can be simpler

extension Reactive<LSCircleProgressView> {

without this.

where Base: LSCircleProgressView

Even with this action, the compiling is Successful!

Please give me 👏 if this post is helpful to you.
There are more posts about iOS.

Please visit my Linked-In profile if you want to know more about me.

--

--

No responses yet