【问题标题】:NSTreeController is not setting Core Data children attributesNSTreeController 没有设置核心数据子属性
【发布时间】:2012-03-12 10:45:43
【问题描述】:

我的 NSTreeController 正在设置 Core Data 父属性而不是子属性。

我有一个这样的核心数据模型

Item
    title (string)
    isLeaf (boolean,readonly)
    parent -> Group (inverse children)

Group (parent entity Item)
    children -> Item (inverse parent)

Leaf (parent entity Item)

然后我使用 NSOutlineView 并使用 NSTreeController 显示它。我使用方法 -[NSTreeController insertObject:atArrangedObjectIndexPath:] 尝试插入 Leaf,叶子父属性设置正确但父 Group.children 属性仍然设置为 nil。

当我运行项目时,我在控制台上收到以下警告

Warning: <NSTreeController: 0x7fcd93c19d20>[object class: NSMutableDictionary] childrenKeyPath cannot be nil. To eliminate this log message, set the childrenKeyPath attribute in Interface Builder

即使 childrenKeyPath 在界面生成器中设置,为什么当我有一个 NSManagedObject 子类 SavedItem、SavedGroup 和 SavedLeaf 时它使用 NSMutableDictionary。我已经下载了几个示例,但我看不到他们在做什么,而我没有。

【问题讨论】:

    标签: objective-c cocoa core-data nsoutlineview nstreecontroller


    【解决方案1】:

    其他人的解决方案是我试图变得聪明,而不是在实际的核心数据定义中定义 isLeaf 属性。我试图在实现中将其硬编码为返回 truefalse,具体取决于它是 Group 还是 Leaf,出于某种原因,NSTreeController 不喜欢这样,即使您还在基类中定义了它。我已经覆盖了 awakeFromInsert 方法并将其设置在那里。

    另外有趣的是,如果您在基类中定义 children 属性不做任何事情/返回 nilNSTreeController 也不喜欢它>(即,Group 子类可以覆盖它),children 实体将被保存但不会显示。这是我在尝试解决最初的问题时所做的事情,我认为这是人们可能想知道的事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-27
      • 1970-01-01
      • 2014-10-06
      • 2020-04-18
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多