/dev/trouble
Eric Roller's Development Blog

In a custom NSArrayController, an action was only to be done when we had a selection. The initial attempt was along the lines of:

if ([self selection])
{
    // do something
}

That, however, does not work since even an empty selection returns a proxy object:

(gdb) print-object [self selection]
<_NSControllerObjectProxy: 0x5bfa60>
(gdb) print-object [[self selection] valueForKeyPath:@"db.name"]

So I checked the NSObjectController documentation and was reminded that a NSNoSelectionMarker can be returned by the function, making me think it that the if statement could be changed like this (NOT):

if ([self selection] != NSNoSelectionMarker)
{
    // DOES NOT WORK
}

However, this does not work (but checking the return value of valueForKeyPath: might).

The most elegant solution for an NSArrayController would be to ignore the selection and just to check the (first) selectionIndex like this:

if ([self selectionIndex] != NSNotFound)
{
    // do something
}