我在我的代码中发现了一个奇怪的错误,但我无法随时重现它。有时,我的 iPad 应用程序因以下键路径错误而崩溃:
Keypath contentSize changed
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<UIWebDocumentView: 0x1c9fce00; frame = (0 0; 1034 75); text = 'coucou'; opaque = NO; userInteractionEnabled = NO; layer = <UIWebLayer: 0x1daa9760>>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: contentSize
Observed object: <UITextView: 0x1da83e60; frame = (32 32; 1034 198); text = 'coucou'; layer = <CALayer: 0x1dad2650>; contentOffset: {0, -62}>
Change: {
kind = 1;
new = "NSSize: {1034, 75}";
}
这是处理 keypath 观察的代码:
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object != textViewCurrentlyEditing)
return;
NSLog(@"Keypath %@ changed", keyPath);
UITextView *tv = object;
UIObject *selected = (__bridge UIObject *)context;
[self updateTextViewAlign:tv forObject:selected];
}
这是我附上它的地方:
UITextView *tv = [[UITextView alloc] initWithFrame:button.frame];
[tv addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:(void *)selected];
textViewCurrentlyEditing = tv;
'selected' 是 UIObject 类型。
我不在代码中的任何地方使用 UIWebLayer。为什么从 UITextView 到 UIWebLayer 的对象突变?我在做什么?