Kez*_*zer 21 objective-c key-value-observing
运行项目时,我在输出窗口中收到以下内容:
An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: connection.messageQueue
Observed object: <Client: 0x10011ddb0>
...
Run Code Online (Sandbox Code Playgroud)
你明白了.问题是,我不知道为什么会这样.然而,似乎一切正常.这是导致问题的代码:
-(id) initWithIdentifier:(NSString*)ident andClient:(Client*)chatClient {
if(![super initWithNibName:@"ChatView" bundle:nil]) {
return nil;
}
[self setTitle: ident];
client = chatClient;
[self setIdentifier:ident];
inContext = false;
[client addObserver:self forKeyPath:@"connection.messageQueue" options:NSKeyValueObservingOptionNew context:NULL];
return self;
}
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSAttributedString* rar = [[NSAttributedString alloc] initWithString:@"test"];
[[textView textStorage] appendAttributedString:rar];
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
Run Code Online (Sandbox Code Playgroud)
我以为我会显示与此相关的所有代码.该类只有几个方法,所以这就是你需要看到的.我甚至没有使用这个改变,我只是在KVO事件被解雇时采取"测试".
随着消息一直进入,堆栈跟踪变得非常快.但似乎一切都运转正常.
有线索吗?
Rob*_*ier 48
[super observeValueForKeyPath:ofObject:change:context:]如果您处理通知,则不应致电.你应该只为你自己处理的通知打电话.
Dmi*_*aev 14
我有同样的问题:-observeValueForKeyPath:ofObject:change:context:收到消息但未处理.我在这里搜索过,然后我自己想出来了,所以也许它可以帮助别人.
对我来说,问题是:注册为观察者的对象(如果你有... addObserver:self ...那么该对象是self)在值被更改时被释放.但观察员仍然登记,所以零观察员-observeValueForKeyPath:ofObject:change:context:收到了消息.
很容易意外地使用代码完成来override class func observeValue...而不是override func observeValue...,这就是我的案例中的问题。换句话说,确保您正在实施正确的覆盖。
| 归档时间: |
|
| 查看次数: |
11371 次 |
| 最近记录: |