Nibin V
Nibin V

Reputation: 582

RxSwift to Combine: Listening for UIView property update

I want to listen for the isHidden property of the UIView and based on the changes in the value, I am notifying the respective delegates.

I trying to convert the following Rx code to Combine and I am not sure how.

Sample Rx Code:

customView.rx
   .observe(Bool.self, "hidden")
   .subscribe(onNext: { [weak self] value in
      guard let self = self else { return }
      self.view.isHidden = value ?? true
      self.delegate?didUpdate(isHidden: value ?? true)
   })
   .disposed(by: disposeBag)

Kindly share a few ideas or suggestions.

Note: I am pretty new to RxSwift and Combine Framework.

Upvotes: 1

Views: 1263

Answers (1)

Craig Siemens
Craig Siemens

Reputation: 13266

NSObject has a KeyValueObservingPublisher that you can subscribe to to get changes to KVO properties. You can create an instance of it yourself or use one of the publisher(...) methods to create one.

view.publisher(for: \.isHidden)
// or
view.publisher(for: \.isHidden, options: [.initial, .new])

Upvotes: 2

Related Questions