【问题标题】:Cannot perform operation because childrenKeyPath is nil无法执行操作,因为 childrenKeyPath 为 nil
【发布时间】:2013-09-17 14:54:59
【问题描述】:

当我尝试将孩子插入我的 NSTreeController 时,为什么会出现此崩溃错误?

NSTreeController *tree = [NSTreeController new];
NSTreeNode *node1 = [NSTreeNode treeNodeWithRepresentedObject:@{ @"type": @"shirt" }];

// The below method causes mysterious crash
[tree insertObject:node1 atArrangedObjectIndexPath:[NSIndexPath indexPathWithIndex:0]];

Xcode 说:

* 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“[对象类:NSMutableDictionary] 无法执行操作,因为 childrenKeyPath 为 nil” *

为什么会导致这个错误?什么是 childrenKeyPath,我为什么需要它(我没有使用界面生成器)?

【问题讨论】:

    标签: objective-c macos crash osx-lion nstreecontroller


    【解决方案1】:

    我在这里犯了几个错误。您不应该将NSTreeNode 类型的实例插入带有insertObjectNSTreeController。您使用insertObject 插入模型对象,此时将自动为其创建NSTreeNode

    此外,在您的模型对象中,您需要有一个设置为NSMutableArray 的属性或键。然后,在插入任何模型对象之前,将NSTreeControllerchildrenKeyPath 属性设置为等于该属性或键的名称。

    这是因为NSTreeControllerNSTreeNode 本身并不是为保存数据或子对象而设计的,而是作为您已创建和保留的模型的简单但有用的“地图” .

    我希望这些信息对其他人有所帮助,因为 StackOverflow 的其余部分对此保持沉默……

    【讨论】:

      猜你喜欢
      • 2019-01-01
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 2015-02-27
      • 1970-01-01
      相关资源
      最近更新 更多