CombineCommunity / CombineCocoa

Combine publisher bridges for UIKit
MIT License
1.66k stars 131 forks source link

Can't create my own delegate-based publishers #28

Closed kevinrenskers closed 4 years ago

kevinrenskers commented 4 years ago

I'm trying to add some publishers to UISearchBarDelegate like this:

extension UISearchBar {
  var searchBarSearchButtonClicked: AnyPublisher<UISearchBar, Never> {
    let selector = #selector(UISearchBarDelegate.searchBarSearchButtonClicked(_:))

    return delegateProxy
      .interceptSelectorPublisher(selector)
      .map { $0[1] as! UISearchBar }
      .eraseToAnyPublisher()
  }

  private var delegateProxy: UISearchBarDelegateProxy {
      .createDelegateProxy(for: self)
  }
}

private class UISearchBarDelegateProxy: DelegateProxy, UISearchBarDelegate, DelegateProxyType {
  func setDelegate(to object: UISearchBar) {
    object.delegate = self
  }
}

Sadly I am getting two compiler errors:

'interceptSelectorPublisher' is inaccessible due to 'internal' protection level

and

Cannot inherit from non-open class 'DelegateProxy' outside of its defining module

Screen Shot 2020-10-01 at 13 50 29

freak4pc commented 4 years ago

Fair point, we'll have to make DelegateProxy open and interceptSelectorPublisher public. If I give you a branch will you be able to test it?

kevinrenskers commented 4 years ago

No problem

freak4pc commented 4 years ago

By the way it might be nicer to push such a proxy as a PR so we could offer it to everyone ;) Outside the point, but still relevant

freak4pc commented 4 years ago

@kevinrenskers please try expose-delegateproxy

kevinrenskers commented 4 years ago

That was definitely my plan, once I had it done in my own project :)

kevinrenskers commented 4 years ago

Alrighty, that works.