【问题标题】:"this class is not key value coding-compliant for the key"“此类不符合键的键值编码”
【发布时间】:2012-11-15 12:03:02
【问题描述】:

我觉得我在这里遗漏了一些明显的东西,但我是 obj-c 的新手,所以也许这只是我不知道的东西。

我在运行时遇到异常错误...

NSDictionaryI 0x9d384d0> setValue:forUndefinedKey:]: 这个类不是 键 overObject 的键值编码兼容。'

在此代码的第 4 行...

NSDictionary *tempDictionary = [[NSDictionary alloc] init];

    Boolean overObjectYES = NO;
    Boolean overObjectNo = NO;

    [tempDictionary setValue:[NSNumber numberWithBool:overObjectYES] forKey:@"overObject"];

【问题讨论】:

    标签: objective-c ios


    【解决方案1】:

    我认为这是在您的意思是NSMutableDictionary 时使用NSDictionary 和在setObject 通常认为更合适时发送消息setValue 的组合。有关详细信息,请参阅此问题:

    NSDictionary setValue:forKey: -- getting "this class is not key value coding-compliant for the key"

    【讨论】:

    • 这里有点糊涂了,所以SetValue,为key设置对象的内容?而 SetObject 集合是键的实际对象本身?
    • @Phil 忽略 setValue 方法,它是一个更大、更复杂的主题的一部分,称为键值编码。改用 setObject(这是 NSMutableDictionary 定义的方法)
    • 老实说,我不确定为什么会有这两条消息 - Apple 的类参考建议 setValue 使用 setObject 但如果传递了 nil 值,则会删除键值对,而 @ 987654329@ 引发NSInvalidArgumentException NSMutableDictionary Class Reference
    • @KendallLister 第一个是称为 NSKeyValueCoding 协议的一部分。任何 NSObject 都可以接受它,但是某些类(例如 NSMutableDictionary)会覆盖它的行为。
    【解决方案2】:

    你的意思是tempDictionary 是一个可变 字典吗?如果是,则声明为:

    NSMutableDictionary *tempDictionary = [[NSMutableDictionary alloc] init];
    

    【讨论】:

      【解决方案3】:

      使用NSMutableDictionaryNSDictionary 不可编辑。

      NSMutableDictionary *tempDictionary = [[NSMutableDictionary alloc] init];
      Boolean overObjectYES = NO;
      Boolean overObjectNo = NO;
      
      [tempDictionary setValue:[NSNumber numberWithBool:overObjectYES] forKey:@"overObject"];
      

      【讨论】:

        猜你喜欢
        • 2017-10-15
        • 2019-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-03
        • 1970-01-01
        相关资源
        最近更新 更多