定义生成 KeyValueObservingPublisher 的 `publisher(for:)` 方法的 header 在哪里?

Where is the header that defines the `publisher(for:)` method that yields a KeyValueObservingPublisher?

我可以看到定义 NSObject.KeyValueObservingPublisher 的基金会 header。但是我找不到 header 定义 publish(for:) 方法的 returns 这个发布者。而且我在文档中找不到它。

肯定涉及到NSObject,因为用KVO只能观察到一个NSObject。它必须以 publisher(for:)publisher(for:options:) 的形式出现,因为这就是代码完成在 Xcode 中为我提供的。但是 代码完成从哪里 获取信息?

它不在文档中,当您跳转到 headers 时,它在 Xcode 中不可见。然而,我发现它隐藏在 .swiftinterface 文件中 Xcode:

@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension _KeyValueCodingAndObservingPublishing where Self : ObjectiveC.NSObject {
    public func publisher<Value>(for keyPath: Swift.KeyPath<Self, Value>, options: Foundation.NSKeyValueObservingOptions = [.initial, .new]) -> ObjectiveC.NSObject.KeyValueObservingPublisher<Self, Value>
}

所以声明存在但没有记录。

我认为这里的问题是这个方法是在协议 _KeyValueCodingAndObservingPublishing 的扩展中声明的,下划线使文档和生成的接口不可见。我认为这是一个错误。

EDIT Rob Mayoff 在评论中指出,您还可以在 https://github.com/apple/swift/blob/master/stdlib/public/Darwin/Foundation/Publishers%2BKeyValueObserving.swift.

查看源代码