# The peculiar case of key-value observing function prototype

**URL:** <https://forums.swift.org/t/the-peculiar-case-of-key-value-observing-function-prototype/58083>\
**Category:** Using Swift\
**Created:** [June 13, 2022, 1:43pm UTC](https://forums.swift.org/t/the-peculiar-case-of-key-value-observing-function-prototype/58083 "2022-06-13T13:43:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [June 13, 2022, 1:43pm UTC](https://forums.swift.org/t/the-peculiar-case-of-key-value-observing-function-prototype/58083/1 "2022-06-13T13:43:20Z")

</div>

In this code opened in Xcode window:

```
observer = AVPlayer(url: url).observe(\.timeControlStatus, options: .new) { player, change in
    ...
}

```

option click on "observe" shows some minimalistic documentation (`keyPath` - No description, `options` - No description). ok - better than nothing. Command click opens some autogenerated swift code which doesn't have "observe" function in it (although there is the relevant "NSKeyValueObservedChange" in that file). Why is this the case and what do I do to view "correct" autogenerated file? It is an iOS target if it matters.

(Interestingly, I also failed to find the reference page for this call on Apple site (e.g. it is not listed [here](https://developer.apple.com/documentation/objectivec/nsobject/nskeyvalueobserving)) other than in this [overview](https://developer.apple.com/documentation/swift/using-key-value-observing-in-swift)).

I wonder if there's some magic in Swift to autogenerate this call from another, e.g. this: `func observeValue(forKeyPath:of:change:context:)`?

---

<div class="post-metadata">

**Author:** ![mayoff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mayoff/32/613_2.png) [@mayoff](https://forums.swift.org/u/mayoff)\
**Post date:** [June 13, 2022, 4:42pm UTC](https://forums.swift.org/t/the-peculiar-case-of-key-value-observing-function-prototype/58083/2 "2022-06-13T16:42:03Z")

</div>

The `observe` method has always lacked documentation, and it has always been difficult to even find its declaration.

The problem, I believe, is that the method is not actually defined on `NSObject`. It's defined on the `_KeyValueCodingAndObserving` protocol, and Xcode tries to hide the existence of SDK identifiers that start with `_`.

You can find the implementation of `observe` as of Swift 5.4 here: [`NSObject.swift`](https://github.com/apple/swift/blob/845205b4407cb1e50ebe69a4d1174714338337c3/stdlib/public/Darwin/Foundation/NSObject.swift#L254). It was removed from the Swift repo in Swift 5.5, but I doubt the implementation has changed much.

I believe the method is part of a protocol (rather than being defined directly on `NSObject`) is because that is how you make the `Self` argument to the closure take on the lexical type of the observed object (in your case, `AVPlayer`), rather than always being `NSObject`.
