0

When I select an NSTreeController in Interface Builder (in Xcode 4.6.3) and look at the attributes inspector, I see two sections named Tree Controller and Object Controller. The Tree Controller part makes some sense, but I'm having trouble finding an explanation of the Object Controller section. The first item is "Mode", with options "Class" and "Entity Name", and I think that you use the latter when using Core Data and the former when not. I'm not using Core Data. So, what's the meaning of the next item, Class Name? It defaults to NSMutableDictionary, but I thought the standard tree node type was NSTreeNode. Specifically, if I want my tree node to be an NSTreeNode whose represented object is an NSMutableDictionary, how would I fill in the Object Controller section?


Post-answer afterthought: Although I now see that one can use nodes of type NSMutableDictionary to build the content tree for an outline view, there are advantages to making a subclass of NSTreeNode. First, NSTreeNode automatically keeps track of parent links, which can be useful in some cases. Second, I can define my data members as properties, and then access them using dot notation rather than objectForKey: messages.

JWWalker
  • 22,385
  • 6
  • 55
  • 76

1 Answers1

2

The Object Controller panel is simply Interface Builder's way of allowing you to set either the entityName property of NSTreeContoller or the objectClass property. (NSTreeController is a subclass of NSObjectController, which is where these properties are defined.)

NSTreeNode is a class used by Cocoa to wrap your objects (or entities) before they're placed in the tree. You have no say in this process, it happens automatically and is thus nothing to do with the Attributes Inspector. What's more you'll rarely (never?) need to create an NSTreeNode instance yourself - though you will interact with them pretty regularly.

So it looks like you don't actually need to do anything in this panel - the default values are what you're after. Of course, you'll still need to fill in the Key Paths section.

Here's a very simple demo project (created with Xcode 6.3). Hopefully this will help.

Paul Patterson
  • 6,840
  • 3
  • 42
  • 56
  • I'd add that NSTreeController's `content` is the *root* object or array of the tree. The tree controller wraps each object that it finds in the `childrenKeyPath` *collection* in an NSTreeNode. So with an objectClass of NSMutableDictionary, and a childrenKeyPath of allValues, you'll get NSTreeNode representedObjects of type NSMutableDictionary for non-leaf nodes, but other types for leaf nodes. – stevesliva Apr 16 '15 at 04:23
  • I'm still way confused. (I haven't been able to find an example of using a view-based outline view with bindings but not Core Data.) If you say I shouldn't need to create tree nodes myself, does that mean you do not recommend using `setContent:`? Then how do I get data into the table? I tried saying `[self.treeController add: self]` and then `[self.outlineView reloadData]`, but thereafter `[self.outlineView numberOfRows]` returns 0, yet it looks like there is a row in the table. – JWWalker Apr 17 '15 at 20:33
  • I've put up a sample project on my GitHub account - see link in updated answer. – Paul Patterson Apr 18 '15 at 20:54