Combine API for intercepting Objective-C selector invocations in Swift.
Many Cocoa APIs rely on delegate callbacks and Objective-C selectors.
While Combine provides powerful tools for working with asynchronous streams, it does not offer built-in support for observing selector-based APIs. Integrating such APIs often requires writing custom delegate proxies or bridging imperative callbacks into publishers manually.
This package provides a Combine-friendly API for intercepting Objective-C selector invocations, allowing delegate-style APIs to be observed and transformed using publishers.
Observe selectors on NSObject instances
navigationController.intercept(_makeMethodSelector(
selector: UINavigationController.popViewController,
signature: navigationController.popViewController
))
.sink { result in
print(result.args) // `animated` flag
print(result.output) // popped `UIViewController?`
}You can also simplify creating method selector with CombineInterceptionMacros if you are open for macros
navigationController.intercept(
#methodSelector(UINavigationController.popViewController)
).sink { result in
print(result.args) // `animated` flag
print(result.output) // popped `UIViewController?`
}Macros require
swift-syntaxcompilation, so it will affect cold compilation time
If you use it to create a library it may be a good idea to export this one implicitly
// Exports.swift
@_exported import CombineInterceptionIt's a good idea to add a separate macros target to your library as well
// Exports.swift
@_exported import CombineInterceptionMacrosYou can add CombineInterception to an Xcode project by adding it as a package dependency.
- From the File menu, select Swift Packages › Add Package Dependency…
- Enter
"https://github.com/capturecontext/combine-interception.git"into the package repository URL text field - Choose products you need to link them to your project.
If you use SwiftPM for your project, you can add CombineInterception to your package file.
.package(
url: "https://github.com/capturecontext/combine-interception.git",
.upToNextMinor(from: "0.4.0")
)Do not forget about target dependencies:
.product(
name: "CombineInterception",
package: "combine-interception"
).product(
name: "CombineInterceptionMacros",
package: "combine-interception"
)Note
The package is compatible with non-Apple platforms, however this package uses conditional compilation, so APIs are only available on Apple platforms
This library is released under the MIT license. See LICENSE for details.
See ACKNOWLEDGMENTS for inspiration references and their licences.