这个observeValueForKeyPath有什么问题:ofObject:change:context:implementation? [英] What's wrong with this observeValueForKeyPath:ofObject:change:context: implementation?
问题描述
在我的UIScrollView子类中,我正在观察帧更改:
In my UIScrollView subclass, I'm observing frame changes:
[self addObserver:self forKeyPath:@"frame" options:0 context:NULL];
我的 observeValueForKeyPath:ofObject:更改:context:
实现如下:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == self && [keyPath isEqualToString:@"frame"]) {
[self adjustSizeAndScale];
}
if ([UIScrollView instancesRespondToSelector:@selector(observeValueForKeyPath:ofObject:change:context:)]) {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; // Exception
}
}
但是这个代码我得到了异常:
But I get exception with this code:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<WLImageScrollView: 0x733a440; baseClass = UIScrollView; frame = (0 0; 320 416); clipsToBounds = YES; layer = <CALayer: 0x7346500>; contentOffset: {0, 0}>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: frame
Observed object: <WLImageScrollView: 0x733a440; baseClass = UIScrollView; frame = (0 0; 320 416); clipsToBounds = YES; layer = <CALayer: 0x7346500>; contentOffset: {0, 0}>
Change: {
kind = 1;
}
Context: 0x0'
这是否意味着UIScrollView实现 observeValueForKeyPath:ofObject:change:context:
但抛出上述异常?
Does it mean UIScrollView implements observeValueForKeyPath:ofObject:change:context:
but throws the above exception?
如果是这样,我该怎样才能正确实现 observeValueForKeyPath:ofObject:change:context:
这样我就可以处理我感兴趣的更改并让超类有机会处理它感兴趣的更改?
If so, how can I properly implement observeValueForKeyPath:ofObject:change:context:
so that I can both handle my interested changes and give superclass a chance to handle its interested changes?
推荐答案
编辑:BJ荷马的答案可能是一个更好的方法来接受这里;我忘记了上下文参数了!
即使调用超级实现是按书本,它似乎调用 observeValueForKeyPath:ofObject:change:context:
on UIKit
实际上没有观察到相关字段的类会抛出 NSInternalInconsistency
异常(不是 NSInvalidArgumentException
你会得到一个无法识别的选择器)。异常中的关键字符串表示收到但未处理。
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".
据我所知,没有详细记录的方法可以找出对象是否在给定的密钥路径上观察另一个对象。可能存在部分记录的方式,例如 -observationInfo
属性,据说它可以携带有关对象观察者的信息,但是你自己就是这样 - 它是一个 void *
。
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 *
.
所以我看到它,你有两个选择:要么不要打电话给 super
实施或使用 @try
/ @catch
/ @finally
阻止忽略特定类型的 NSInternalInconsistencyException
。第二种选择可能更具未来性,但我有预感,一些侦探工作可以通过第一个选项让你获得更令人满意的结果。
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.
这篇关于这个observeValueForKeyPath有什么问题:ofObject:change:context:implementation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!