Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

combine

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?

like image 757
matt Avatar asked Oct 31 '25 04:10

matt


1 Answers

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-corelibs-foundation/blob/main/Darwin/Foundation-swiftoverlay/Publishers%2BKeyValueObserving.swift.

like image 62
matt Avatar answered Nov 02 '25 01:11

matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!