What's wrong with this observeValueForKeyPath:ofObject:change:context: implementation?

后端 未结 2 1290
终归单人心
终归单人心 2021-02-05 14:45

In my UIScrollView subclass, I\'m observing frame changes:

[self addObserver:self forKeyPath:@\"frame\" options:0 context:NULL];

My obser

2条回答
  •  抹茶落季
    2021-02-05 15:41

    Edit: BJ Homer's answer is probably a better approach to take here; I forgot all about the context parameter!

    Even though calling the super implementation is by-the-book, it seems like calling observeValueForKeyPath:ofObject:change:context: on UIKit classes that don't actually observe the fields in question throws an NSInternalInconsistency exception (not the NSInvalidArgumentException you would get with an unrecognized selector). The key string in the exception that suggests this to me is "received but not handled".

    As far as I know, there's no well-documented way to find out if an object observes another object on a given key path. There may be partially-documented ways such as the -observationInfo property which is said to carry information on the observers of an object, but you're on your own there—it's a void *.

    So as I see it, you've got two options: either don't call the super implementation or use an @try/@catch/@finally block to ignore that specific type of NSInternalInconsistencyException. The second option is probably more future-proof, but I have a hunch that some detective work could get you more satisfying results via the first option.

提交回复
热议问题