I am currently using rxswift, but am studying the combine framework.
I used the methodInvoked function a lot in rxswift.
Is it possible to create functionality like methodInvoked method in combine framework?
public extension Reactive where Base: UIViewController {
var viewWillAppear: Observable<[Any]> {
return methodInvoked(#selector(UIViewController.viewWillAppear(_:)))
}
var viewDidAppear: Observable<[Any]> {
return methodInvoked(#selector(UIViewController.viewDidAppear(_:)))
}
var viewDidDisappear: Observable<[Any]> {
return methodInvoked(#selector(UIViewController.viewDidDisappear(_:)))
}
var viewWillDisappear: Observable<[Any]> {
return methodInvoked(#selector(UIViewController.viewWillDisappear(_:)))
}
}
I want to implement the methodInvoked function like the code above using the combine framework.
Yes, it is possible to reproduce that functionality for methods written in Objective-C. The technique for replacing an Objective-C method with a custom implementation (that may, or may not, invoke the original method definition) is often called "Monkey Patching" or "Method Swizzling" and you can find techniques for doing so through Google. Here is an example:
https://nshipster.com/method-swizzling/
To implement the same thing with combine, you would probably use a
PassthroughSubjectthat gets type erased to anAnyPublisherand have your replacement Objective-C methodsenda value to the subject each time the method is invoked.