0

我正在使用以下代码添加观察者:

[self.priorityQueue addObserver:self forKeyPath:@"operations" options:options context:NULL];

并且观察方法定义为:

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                     change:(NSDictionary *)change context:(void *)context
{
    if ([object isKindOfClass:[NSOperationQueue class]] && [keyPath isEqualToString:@"operations"]) {
        //calling my method
    }
    else {       
        [super observeValueForKeyPath:keyPath ofObject:object
                      change:change context:context];
    }
}

但我收到以下错误:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“(上下文:0x0,属性:0x110bf60>):已收到 -observeValueForKeyPath:ofObject:change:context: 消息但未处理。关键路径:操作观察对象:{name = 'NSOperationQueue 0x11213c0'} 更改:

请告诉我,如果我在观察者方法中犯了一些错误。

谢谢,

4

1 回答 1

0

NSObject 没有实现那个功能——如果你扩展 NSObject 永远不要调用 super。

来自苹果文档:

 Be sure to call the superclass's implementation *if it implements it*.
 NSObject does not implement the method.

https://developer.apple.com/library/ios/documentation/cocoa/conceptual/KeyValueObserving/Articles/KVOBasics.html

于 2013-10-09T14:47:47.820 回答