ios - 使用 KVO 时出现 NSInternalInconsistencyException

标签 ios cocoa-touch cocoa ios4 key-value-observing

我正在尝试使用从 iPhone 教程书中获得的 KVO 示例,但出现此异常

   Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<UINavigationController: 0x139630>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: dataUpdated
Observed object: <FilterDetailsController: 0x1b9930>
Change: {
    kind = 1;
}
    Context: 0x0'
     Call stack at first throw:
    (
        0   CoreFoundation                      0x320d3987 __exceptionPreprocess + 114
        1   libobjc.A.dylib                     0x3271849d objc_exception_throw + 24
        2   CoreFoundation                      0x320d37c9 +[NSException raise:format:arguments:] + 68
        3   CoreFoundation                      0x320d3803 +[NSException raise:format:] + 34
        4   Foundation                          0x35c316e9 -[NSObject(NSKeyValueObserving) observeValueForKeyPath:ofObject:change:context:] + 60
        5   Foundation                          0x35bd4a3d NSKeyValueNotifyObserver + 216
        6   Foundation                          0x35bd46e5 NSKeyValueDidChange + 236
        7   Foundation                          0x35bcc3f5 -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] + 76
        8   Foundation                          0x35c30d87 _NSSetObjectValueAndNotify + 98
        9   Lucid Dreaming App                  0x000108e3 -[FilterDetailsController adjustFilterDetails:] + 138
        10  CoreFoundation                      0x3207afed -[NSObject(NSObject) performSelector:withObject:withObject:] + 24
        11  UIKit                               0x323b3ea5 -[UIApplication sendAction:to:from:forEvent:] + 84
        12  UIKit                               0x323b3e45 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 32
        13  UIKit                               0x323b3e17 -[UIControl sendAction:to:forEvent:] + 38
        14  UIKit                               0x323b3b69 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 356
        15  UIKit                               0x323b43c7 -[UIControl touchesEnded:withEvent:] + 342
        16  UIKit                               0x323a9d4d -[UIWindow _sendTouchesForEvent:] + 368
        17  UIKit                               0x323a96c7 -[UIWindow sendEvent:] + 262
        18  UIKit                               0x3239499f -[UIApplication sendEvent:] + 298
        19  UIKit                               0x323942df _UIApplicationHandleEvent + 5090
        20  GraphicsServices                    0x35472f03 PurpleEventCallback + 666
        21  CoreFoundation                      0x320686ff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 26
        22  CoreFoundation                      0x320686c3 __CFRunLoopDoSource1 + 166
        23  CoreFoundation                      0x3205af7d __CFRunLoopRun + 520
        24  CoreFoundation                      0x3205ac87 CFRunLoopRunSpecific + 230
        25  CoreFoundation                      0x3205ab8f CFRunLoopRunInMode + 58
        26  GraphicsServices                    0x354724ab GSEventRunModal + 114
        27  GraphicsServices                    0x35472557 GSEventRun + 62
        28  UIKit                               0x323c7d21 -[UIApplication _run] + 412
        29  UIKit  ...

我从

开始
[advancedController addObserver:self.navigationController forKeyPath:@"dataUpdated" options:0 context:nil];

在 self.navigation Controller 中,我定义了观察方法:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{

    NSLog(@"2 Observed change for: %@",keyPath);
//    if (context == <#context#>) {
//        <#code to be executed upon observing keypath#>
//    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
//    }
}

在advancedController中我有一个NSNumber属性

 @property(nonatomic,retain)NSNumber* dataUpdated;

当我按下按钮时,我希望观察者会开火。

  - (IBAction)adjustFilterDetails:(id)sender {
        self.dataUpdated = [NSNumber numberWithInt:[self.dataUpdated intValue]+1];
    }

我是否需要实现某些协议(protocol)或明确声明我将更新该值?我读到 NSKeyValueObserving 是一个“非正式”协议(protocol)。感谢您的帮助!

我已经找到了自己问题的答案。我在 ViewController 中定义了 KVO 协议(protocol)方法,但是,我不小心为 View Controller 注册了导航 Controller 来观察该值。通过获取 View Controller 可以找到正确的对象,如下所示:

 [advancedController addObserver:(RootViewController*)self.navigationController. topViewController forKeyPath:@"dataUpdated" options:0 context:nil];

最佳答案

您的问题是对 super 的调用。对于您自己观察到的属性,您不应将此方法传递给 super。您注释掉了可以帮助您完成此操作的代码。

一个可能的正确观察是(注意上下文):

[advancedController addObserver:self.navigationController forKeyPath:@"dataUpdated" options:0 context:self];

那么正确的观察者是:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{
    if (context == self) {
        NSLog(@"2 Observed change for: %@",keyPath);
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

Dave Dribin 在 Proper Key-Value Observer Usage 中提供了另一种正确使用上下文的方法,以及为什么需要这种方法的背景故事。 .

关于ios - 使用 KVO 时出现 NSInternalInconsistencyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7599979/

相关文章:

ios - 使用 SDWebImage 时表格 View 滚动滞后

iphone - 从 appDelegate 更新 UILabel

iphone - 如何将单元格中的第三个值传递给另一个 TableView Controller

iphone - GKSession 问题 - Objective-C GameKit

iphone - 在应用程序内购买积分以在外部网站中使用?

macos - 如何设置 NSTextView 中所有文本的颜色(而不仅仅是随后输入的文本)

objective-c - 如何获取 Mac OS X 上的默认临时目录?

objective-c - 代表的常见位置有哪些?

ios - 如何快速保存钥匙串(keychain)中的名称和密码详细信息

iphone - 在 iOS 上加载压缩图像的最快方法是什么?