Comments on: The Case of the Missing Check http://www.red-sweater.com/blog/161/the-case-of-the-missing-check Mac & Technology Writings by Daniel Jalkut Wed, 7 Jan 2009 00:27:45 +0000 http://wordpress.org/?v=2.7 hourly 1 By: Jon Hess http://www.red-sweater.com/blog/161/the-case-of-the-missing-check/comment-page-1#comment-52848 Jon Hess Wed, 07 Feb 2007 03:51:33 +0000 http://www.red-sweater.com/blog/161/the-case-of-the-missing-check#comment-52848 Hey Matt - As others have noted, you probably don't mean to invoke ([[self superclass] instancesRespondToSelector:...]). If you are a Foo, and Foo is an Object, this works. If someone subclasses Foo as Bar, then '[self superclass[' is Foo, and Foo always responds to that selector. As others have noted, you probably want to hardwire the classname. Hey Matt -

As others have noted, you probably don’t mean to invoke ([[self superclass] instancesRespondToSelector:…]).

If you are a Foo, and Foo is an Object, this works. If someone subclasses Foo as Bar, then ‘[self superclass[’ is Foo, and Foo always responds to that selector. As others have noted, you probably want to hardwire the classname.

]]>
By: matt neuburg http://www.red-sweater.com/blog/161/the-case-of-the-missing-check/comment-page-1#comment-10108 matt neuburg Mon, 24 Jul 2006 17:52:53 +0000 http://www.red-sweater.com/blog/161/the-case-of-the-missing-check#comment-10108 Funny this should arise just now, since I just got thru posting a note to Cocoa-Dev about this same issue in awakeFromNib. Sometimes super is a built-in Cocoa class and does significant work in awakeFromNib. So that's two bugs in the Cocoa docs, because (1) the Cocoa docs explicitly tell you not to call [super awakeFromNib] if super is a built-in Cocoa class, and (2) the Cocoa docs don't document when a built-in Cocoa class implements awakeFromNib in a significant way. A useful and compact formulation is: if([[self superclass] instancesRespondToSelector:_cmd])   [super awakeFromNib]; Funny this should arise just now, since I just got thru posting a note to Cocoa-Dev about this same issue in awakeFromNib. Sometimes super is a built-in Cocoa class and does significant work in awakeFromNib. So that’s two bugs in the Cocoa docs, because (1) the Cocoa docs explicitly tell you not to call [super awakeFromNib] if super is a built-in Cocoa class, and (2) the Cocoa docs don’t document when a built-in Cocoa class implements awakeFromNib in a significant way. A useful and compact formulation is:

if([[self superclass] instancesRespondToSelector:_cmd])
  [super awakeFromNib];

]]>
By: Marcel Weiher http://www.red-sweater.com/blog/161/the-case-of-the-missing-check/comment-page-1#comment-9884 Marcel Weiher Sat, 22 Jul 2006 10:21:43 +0000 http://www.red-sweater.com/blog/161/the-case-of-the-missing-check#comment-9884 The [super respondsToSelector...] fragement probably doesn't have the effect that you're thinking. It will certainly NOT check wether your superclass implements this method, but actually check wether your class implements that method. super sends the message to self, but starting the message lookup at the superclass of the class where the super send occurs. So it will start looking for respondsToSelector: in NSWindow, in all likelyhood hitting the exact same respondsToSelector: implementation in NSObject that you would have gotten had you sent the message to self. That implementation will then check if the receiver implements the method, which it does (the receiver being your instance of your NSWindow subclass). What you probably want to do is [[self class] superclass] instancesRespondToSelector: ] The [super respondsToSelector...] fragement probably doesn’t have the effect that you’re thinking. It will certainly NOT check wether your superclass implements this method, but actually check wether your class implements that method.

super sends the message to self, but starting the message lookup at the superclass of the class where the super send occurs. So it will start looking for respondsToSelector: in NSWindow, in all likelyhood hitting the exact same respondsToSelector: implementation in NSObject that you would have gotten had you sent the message to self. That implementation will then check if the receiver implements the method, which it does (the receiver being your instance of your NSWindow subclass).

What you probably want to do is [[self class] superclass] instancesRespondToSelector: ]

]]>
By: Daniel Jalkut http://www.red-sweater.com/blog/161/the-case-of-the-missing-check/comment-page-1#comment-9852 Daniel Jalkut Sat, 22 Jul 2006 03:31:35 +0000 http://www.red-sweater.com/blog/161/the-case-of-the-missing-check#comment-9852 Ken: Aha! Jeez, I'm glad I blogged this since I'm sure this is a series of bad habits I would have otherwise held onto for a long time. Ken: Aha! Jeez, I’m glad I blogged this since I’m sure this is a series of bad habits I would have otherwise held onto for a long time.

]]>
By: ken http://www.red-sweater.com/blog/161/the-case-of-the-missing-check/comment-page-1#comment-9849 ken Sat, 22 Jul 2006 03:07:07 +0000 http://www.red-sweater.com/blog/161/the-case-of-the-missing-check#comment-9849 [[self superclass] instancesRespondToSelector:@selector(validateUserInterfaceItem:)] is rarely correct either.  It means that if you ever create a subclass of your (currently) leaf class, the check stops being meaningful. I think you want to hardwire the superclass name. [[self superclass] instancesRespondToSelector:@selector(validateUserInterfaceItem:)] is rarely correct either.  It means that if you ever create a subclass of your (currently) leaf class, the check stops being meaningful.

I think you want to hardwire the superclass name.

]]>
By: Daniel Jalkut http://www.red-sweater.com/blog/161/the-case-of-the-missing-check/comment-page-1#comment-9847 Daniel Jalkut Sat, 22 Jul 2006 02:39:20 +0000 http://www.red-sweater.com/blog/161/the-case-of-the-missing-check#comment-9847 Good catch Adam, you're right. Good catch Adam, you’re right.

]]>
By: Adam Maxwell http://www.red-sweater.com/blog/161/the-case-of-the-missing-check/comment-page-1#comment-9838 Adam Maxwell Sat, 22 Jul 2006 01:28:14 +0000 http://www.red-sweater.com/blog/161/the-case-of-the-missing-check#comment-9838 Shouldn't you use [[self superclass] instancesRespondToSelector:@selector(validateUserInterfaceItem:)] instead of [super respondsToSelector:]? Using the super keyword here seems wrong. Shouldn’t you use [[self superclass] instancesRespondToSelector:@selector(validateUserInterfaceItem:)] instead of [super respondsToSelector:]? Using the super keyword here seems wrong.

]]>
By: Conor Dearden http://www.red-sweater.com/blog/161/the-case-of-the-missing-check/comment-page-1#comment-9758 Conor Dearden Fri, 21 Jul 2006 10:35:41 +0000 http://www.red-sweater.com/blog/161/the-case-of-the-missing-check#comment-9758 In most cases one should call super in methods you override. In most cases one should call super in methods you override.

]]>
By: Daniel Jalkut http://www.red-sweater.com/blog/161/the-case-of-the-missing-check/comment-page-1#comment-9712 Daniel Jalkut Thu, 20 Jul 2006 22:44:33 +0000 http://www.red-sweater.com/blog/161/the-case-of-the-missing-check#comment-9712 Andy: Yep - I think you're right. Somehow I knew I'd get the poetry wrong there :) Andy: Yep - I think you’re right. Somehow I knew I’d get the poetry wrong there :)

]]>
By: Andy Lee http://www.red-sweater.com/blog/161/the-case-of-the-missing-check/comment-page-1#comment-9711 Andy Lee Thu, 20 Jul 2006 22:42:38 +0000 http://www.red-sweater.com/blog/161/the-case-of-the-missing-check#comment-9711 Should that be "expands in inverse proportion"? Should that be “expands in inverse proportion”?

]]>