运行项目时,我在输出窗口中看到以下内容:
An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: connection.messageQueue
Observed object: <Client: 0x10011ddb0>
...你明白了吧。问题是,我不知道为什么会发生这种事。然而,看起来一切都运行得很好。以下是导致问题的代码:
-(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];
}我想我应该展示与此相关的所有代码。这个类只有几个方法,所以这就是您需要看到的全部内容。我甚至没有使用更改,当KVO事件被触发时,我只是在"test“上拨打。
堆栈跟踪变得非常大,非常快,因为消息一直都在进来。然而,看起来一切都很正常。
有什么线索吗?
发布于 2012-09-05 03:55:25
如果您处理通知,则不应调用[super observeValueForKeyPath:ofObject:change:context:]。您应该只为您自己不处理的通知调用此方法。
发布于 2021-06-03 15:37:41
确保你只对你不感兴趣的东西打电话给超级用户。
class Layer: CALayer {
private let key1 = "transport"
private let key2 = "position"
override init() {
super.init()
addObserver(self, forKeyPath: key1, options: [], context: nil)
addObserver(self, forKeyPath: key2, options: [], context: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == key1 || keyPath == key2 {
} else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
}
deinit {
removeObserver(self, forKeyPath: key1)
removeObserver(self, forKeyPath: key2)
}
}https://stackoverflow.com/questions/12270429
复制相似问题