Search code examples
iosswift3reactivekitswiftbond

ReactiveKit migration from v4.x to v5.x


I'm trying to migrate my project to Swift 3. I am using ReactiveKit and Bond and I am struggling with some conversions.

1) Most of my extensions used to look like this:

extension UIView {

public var bnd_superview: Observable<UIView?> {
    return bnd_associatedObservableForValueForKey("superview")
}

I can't find what bnd_associatedObservableForValueForKey was changed to.

2) I have the following code:

self.myButton.bnd_selected.next(false)

it should change to:

self.myButton.reactive.isSelected.next(false)

but I get the following error:

Value of type 'Bond' has no member 'next'


Solution

  • First one can be replaced by Bond type. You can also do it as a ReactiveExtensions extension instead as a UIView extension, so you get .reactive prefix.

    extension ReactiveExtensions where Base: UIView {
    
        public var superview: Bond<UIView?> {
            return bond { view, superview in
                view.superview = superview
            }
        }
    }
    

    You can then bind a signal to your bond.

    let view: SafeSignal<UIView> = ...
    view.bind(to: someView.reactive.superview)
    

    Bonds cannot be observed though, just like there is no way to observe UIView.superview. KVO might work, but UIKit is not KVO compliant.

    As for the second question, isSelected is of type Bond and yes, it does not have the method next. It is used to bind signals.

    You should just do:

    self.myButton.isSelected = false