Combine API 用于在 Swift 中拦截 objc selector。
观察 NSObject 实例上的 selector
navigationController.intercept(_makeMethodSelector(
selector: UINavigationController.popViewController,
signature: navigationController.popViewController
))
.sink { result in
print(result.args) // `animated` flag
print(result.output) // popped `UIViewController?`
}
如果您接受宏,您也可以使用 CombineInterceptionMacros
简化方法 selector 的创建。
navigationController.intercept(
#methodSelector(UINavigationController.popViewController)
).sink { result in
print(result.args) // `animated` flag
print(result.output) // popped `UIViewController?`
}
宏需要
swift-syntax
编译,因此会影响冷启动编译时间。
如果您使用它来创建一个库,那么隐式地导出它可能是一个好主意。
// Exports.swift
@_exported import CombineInterception
最好也为您的库添加一个单独的宏目标。
// Exports.swift
@_exported import CombineInterceptionMacros
您可以通过将 CombineInterception 添加为包依赖项来将其添加到 Xcode 项目中。
"https://github.com/capturecontext/combine-interception.git"
如果您使用 SwiftPM 进行项目开发,您可以将 CombineInterception 添加到您的 package 文件中。
.package(
url: "https://github.com/capturecontext/combine-interception.git",
.upToNextMinor(from: "0.3.0")
)
不要忘记目标依赖项
.product(
name: "CombineInterception",
package: "combine-interception"
)
.product(
name: "CombineInterceptionMacros",
package: "combine-interception"
)
该库是在 MIT 许可证下发布的。 有关详细信息,请参见LICENCE。
有关灵感来源及其许可证,请参见ACKNOWLEDGMENTS。