I suspect you're misunderstanding the purpose of -observationInfo
/-setObservationInfo:
. The value that is set and gotten by those methods is opaque to you. Put differently, its contents are private to the system frameworks and you're not intended to be able to access/parse it. The comment from the docs that you excerpted was:
For improved performance, this method and setObservationInfo: can be
overridden to store the opaque data pointer in an instance variable.
Overrides of this method must not attempt to send Objective-C messages
to the stored data, including retain and release.
Let me try to clarify what this is saying a bit:
For every observation, the frameworks need to keep track of some private data about the observation in order to deliver it later. How it stores that information is not documented, but conceptually, it's probably stored in a map where the key is the pointer value of the object and the value is this opaque data structure.
In specific, performance-sensitive situations, it may be that looking up the observation info in that shared map impacts performance significantly (I've never seen this turn up on a profiling trace myself, but that's not to say it couldn't happen). In those cases, it would be better to store the pointer directly as an instance variable in the object, which changes the fetch operation from a map-lookup to a simple add. From a system frameworks perspective, I would bet that the map-lookup cost is usually far less onerous (in the big picture) than the alternative, which would be to put an instance variable in NSObject for this purpose (which would make every object 8 bytes bigger whether it uses KVO or not -- seems like a no-brainer.)
-observationInfo
/-setObservationInfo:
exist to allow you to make that optimization. If you implemented this optimization, these methods would also be a way to know if your object is being observed or not (i.e. is the info value nil
?) which may allow you to make other changes to your object's behavior.
If you have a need to keep a list of observing objects and details about what they're observing for some other purpose, then you will need to do that yourself by overriding addObserver:...
and removeObserver:...
, and adding code to maintain your own data private structures (while still calling super
.)