Re: Unexpected result from -[super respondsToSelector:]
Re: Unexpected result from -[super respondsToSelector:]
- Subject: Re: Unexpected result from -[super respondsToSelector:]
- From: "Adam R. Maxwell" <email@hidden>
- Date: Fri, 15 Feb 2008 22:24:49 -0800
On Feb 15, 2008, at 10:03 PM, Jerry Krinock wrote:
I've subclassed NSTextView in order to customize its drag
destination behavior. In so doing, I needed to over-ride
awakeFromNib in order to run a little patch. I then dutifully tried
to give super a chance to do its thing...
@implementation SSDragDestinationTextView : NSTextView
- (void)awakeFromNib {
[self patchPreLeopardFocusRingDrawingForScrolling] ;
if ([super respondsToSelector:@selector(awakeFromNib)]) {
NSLog(@"super DOES respond to awakeFromNib") ;
[super awakeFromNib] ;
NSLog(@"It worked.") ;
}
}
...
The above code gives me this head-scratchin' console output:
super DOES respond to awakeFromNib
*** -[SSDragDestinationTextView awakeFromNib]:
unrecognized selector sent to instance 0x14c56f30
I believe that the reference in the exception to
"SSDragDestinationTextView" is an anomoly/bug/artifact in Cocoa's
exception logging. There is no actual instance of 'super'. The
object is really 'self', so that's the class that it prints. I
believe what it means to say is that NSTextView does not respond to -
awakeFromNib.
But what's more surprising is that -respondsToSelector doesn't work
as expected when sent to super. It looks like, if you're dealing
with an informal protocol like NSNibAwaking, for which the
documentation does not state which classes conform to, you've just
got to try it and see if you get an exception, then hard-code
accordingly.
This actually isn't a correct use of the super keyword, although it
seems to make sense at first glance (at least it made sense to me the
first time I tried it :). Instead of calling super's implementation
of respondsToSelector:, try using
if ([[SSDragDestinationTextView superclass]
instancesRespondToSelector:_cmd])
[super awakeFromNib];
which should give the expected result (and no exception).
--
adam
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden