NSTreeController inserts are pretty broke on Leopard

Yes I know, I probably shouldn’t even be using NSTreeController, but tough, I am. On 10.6 I have it working pretty well, but there’s a glaring bug on 10.5:

When inserting or moving an object, the controller’s tree structure will be correct, but objects are always added to the model, not inserted.

For unordered models this is fine, but if your model is based around NSArray, your controller and model become subtly out of sync. For example, asking NSTreeController to insert an object at this index path:

0, 0, 0

will actually insert it at:

0, 0, n+1

where n is the number of children the parent node already had. In the case of Sandvox, the bug tends to only become visible after you close and re-open the document, or have a second controller present observing the same bit of the model.

So what’s the solution? I’ve found it’s fairly straightforward to subclass NSTreeController, check the model’s accuracy after an insert, and correct it like so:

© Mike Abdullah 2007-2015