1

我的 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。我已经下载了几个示例,但我看不到他们在做什么,而我不是。

4

1 回答 1

2

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

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

于 2012-03-15T10:47:11.583 回答