arrayController“valueForUndefinedKey”:为什么它会被立即反映出来?

时间:2011-04-25 10:31:49

标签: objective-c xcode nsarraycontroller

我的应用程序有2个窗口,它们都显示由控制器管理的NSScrollView对象。基本上第一个有数据,第二个有适当的数据聚合。 两者都依赖于NSArrayController类的实现。

我正确地设计了两个窗口(至少我相信)但是我一直收到这个错误:

    [<MyDocument 0x1020257c0> valueForUndefinedKey:]: 
this class is not key value coding-compliant for the key <MY_ARRAY>.

我设法解决这个问题的唯一方法是立即实例化(我的意思是当第一个窗口准备好时)第二个数组并用这样的方式用一些值填充它:

NSMutableArray *newAggregates = [NSMutableArray array];

    [newAggregates addObject:
     [NSMutableDictionary dictionaryWithObjectsAndKeys:
      [NSString stringWithString:@"category"], @"category",
      [NSString stringWithString:@"subcategory"], @"subcategory",
      [NSNumber numberWithFloat:0.0], @"amount_1",
      [NSNumber numberWithFloat:0.0], @"amount_2",
      [NSNumber numberWithFloat:0.0], @"amount_3",
      [NSNumber numberWithFloat:0.0], @"amount_4",
      [NSNumber numberWithFloat:0.0], @"amount_5",
      [NSNumber numberWithFloat:0.0], @"amount_6",
      [NSNumber numberWithFloat:0.0], @"amount_7",
      [NSNumber numberWithFloat:0.0], @"amount_8",
      [NSNumber numberWithFloat:0.0], @"amount_9",
      [NSNumber numberWithFloat:0.0], @"amount_10",
      [NSNumber numberWithFloat:0.0], @"amount_11",
      [NSNumber numberWithFloat:0.0], @"amount_12",
      [NSNumber numberWithFloat:0.0], @"total",
      nil]];

    [self setAggregates:newAggregates];

这样可行,但它不是我想要的,我希望第二个数组只能选择性地执行,当且仅当用户按下某个按钮时。

有没有办法实现这一点,或者应用程序中存在的任何数组控制器必须在开始时实例化?我绝对相信必须有办法避免......

1 个答案:

答案 0 :(得分:1)

我不知道为什么你的解决方法有效(也许你的setAggregates:方法会阻止最终导致错误的调用?)。该错误意味着您在文档对象上绑定了名为<MY_ARRAY>的属性。我猜它可能是你的一个数组控制器的内容绑定。您需要将其更改为您班级中使用的属性的实际名称。