Saturday, June 20, 2009

SourceView’s method [MyWIndowController -changeItemView] uses selectedObjects rather than selectedNodes

After looking more deeply into SourceView’s sample code available on Apple’s web site at http://developer.apple.com/samplecode/SourceView/, I found that the method [MyWIndowController -changeItemView] uses the selectedObjects method of NSTreeController rather than the selectedNodes method. To see this, build the sample code or run the pre-built version of the application in the folder.

  1. Select the first item under PLACES.

  2. Press the “-” button to remove the selected item.

  3. Notice the subview to the right does not change.


In order to see what is intended, change the following code:

NSArray *selection = [treeController selectedObjects];
BaseNode *node = [selection objectAtIndex:0];

into

NSArray* selection = [treeController selectedNodes];
BaseNode* node = [[selection objectAtIndex:0] representedObject];

Since my tests are being run on Mac OS X 10.5.7, I can’t say for sure, but this change may be due to a bug between selectedObjects and selectedNodes or due to the change that happened in 10.4 in [NSTreeController -remove] where the action was deferred until the next runloop iteration.