0

I have an NSOutlineView as a sourcelist, like Mail.app. Its content is bound to an NSTreeController, which manages an array of my model objects. I've implemented drag-and-drop on the sourcelist and this is working perfectly.

Now I'd like to save the new ordering when the user finishes a drag-and-drop action in the sidebar. However, I can't find anything describing how this would usually be done.

The best I can come up with is:

  • add a treeIndex property to my model class, to store the model's position in the sourcelist
  • after a drop action, go through each node in the NSTreeController, and store each node's index in the model's treeIndex property

The problem is, I can't find how to retrieve the arranged objects from the NSTreeController. I can't get treeController.arrangedObjects to give me anything useful!

Searching through Github repos I've seen numerous uses of treeController.arrangedObjects.childNodes!! but that just throws a Value of type 'Any' has no member 'children' syntax error for me.

The following pseudo-code might help describe what I am trying to do after each drop action:

let parentNode = treeController.nodeAtIndex([0, 0])
for childNode in parentNode.children {
   (childNode.representedObject as SidebarItem).indexPosition = childNode.index
}
jeff-h
  • 2,184
  • 1
  • 22
  • 33
  • Which line of the pseudo-code is the problem? – Willeke Apr 22 '17 at 23:16
  • The first line really... although I finally discovered http://stackoverflow.com/a/39661700/2778502 which showed me this: `let treeNodes = (treeController.arrangedObjects as AnyObject).children as [NSTreeNode]?` and that got me going. Therefore for me the focus of the question is now more philosophical (is this the best approach) and less about implementation (what syntax do I need to pull this off). – jeff-h Apr 23 '17 at 23:50

0 Answers0