Question

Right, I'm officially confused.

This is called in a NSTableView subclass on 10.8 and as we can learn from the docs and the headers NSTableView implements NSDraggingSource so all should be good:

 if ([super respondsToSelector:@selector(draggingSession:movedToPoint:)])
    [super draggingSession:session movedToPoint:screenPoint];

When the containing method (override of draggingSession:movedToPoint: in the subclass) is called, the second line however throws the beloved 'unrecognized selector sent to instance 0x1054092c0' exception.

Could anybody please explain what is going on here?!

Was it helpful?

Solution

First, [super respondsToSelector:@selector(draggingSession:movedToPoint:)] is the same as [self respondsToSelector:@selector(draggingSession:movedToPoint:)]. super allows you to invoke the superclass implementation of the given method; in this case, respondsToSelector:. But if your class (or whatever class this object is) does not override -respondsToSelector: (99.9% of classes will not need to override -respondsToSelector:), then the superclass implementation is the same as the implementation of the class of self. Basically, in both cases, you are checking to see if the current object (self) responds to the selector.

So, what you're seeing is this: self responds to the selector, but the superclass of the class this is in does not have an implementation for the selector. What does that mean? Either the current class, or somewhere between the current class as the class of self, this method is implemented. That's why self responds to it. But there is no superclass implementation.

OTHER TIPS

The correct code is actually:

if ([[NSTableView class] instancesRespondToSelector:@selector(draggingSession:movedToPoint:)])
    [super draggingSession:session movedToPoint:screenPoint];

As pointed out by newacct, super refers to the superclass of the class in which the method is implemented, whereas [self superclass] is the superclass of the instance on which it is called, which could be a subclass of your custom class (a grandchild of NSTableView). Of course, it is unlikely you would create such a sub-subclass, but you might as well do things right, and this code is anyway clearer in its intent.

Thanks for the hints -
this does indeed work fine:

 if ([[self superclass] instancesRespondToSelector:@selector(draggingSession:movedToPoint:)])
    [super draggingSession:session movedToPoint:screenPoint];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top