问题
I can see the Foundation header that defines NSObject.KeyValueObservingPublisher
. But I can't find the header that defines the publish(for:)
method that returns this publisher. And I can’t find it in the documentation.
It must involve NSObject, because only an NSObject can be observed with KVO. And it must come in the forms publisher(for:)
and publisher(for:options:)
, because that's what code completion offers me in Xcode. But where is code completion getting that information from?
回答1:
It’s not in the documentation and it’s not visible in Xcode when you jump to the headers. I found it, however, hidden in a .swiftinterface file inside 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>
}
So the declaration exists but it’s undocumented.
I think the problem here is that this method is declared in an extension to a protocol _KeyValueCodingAndObservingPublishing
that is made invisible to the documentation and the generated interface by the underscore. I regard that as a bug.
EDIT Rob Mayoff points out in a comment that you can also look in the source code at https://github.com/apple/swift/blob/master/stdlib/public/Darwin/Foundation/Publishers%2BKeyValueObserving.swift.
来源:https://stackoverflow.com/questions/60381905/where-is-the-header-that-defines-the-publisherfor-method-that-yields-a-keyv