Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: super respondsToSelector




On 30 May '08, at 11:55 PM, Bill Bumgarner wrote:

On May 30, 2008, at 11:43 PM, Jens Alfke wrote:
If you actually want to ask a question of your class's superclass, you have to do so directly:
[[[self class] superclass] instancesRespondToSelector: aSelector]

Well... yeah... you *can* do that.

But *don't do that*. (And I know Jens knows better -- I'm just pointing something out).

Any code that relies upon *skipping* super's implementation of something to get something done is just asking for trouble.

No, that's not what it's for. There are situations where you implement a method, and you want to call the superclass implementation ... but only if there _is_ one, since otherwise that would raise an exception.


This comes about with those @$&% "informal protocols" that pollute the namespace of NSObject. So for example, let's say I subclass NSFoobarControl and implement -awakeFromNib. Now, should I call [super awakeFromNib] first? If NSFoobarControl implements that method, then yes I'd damn better do so, otherwise I screw up the superclass's state. But if it doesn't, that super call will raise an exception.

One solution is to put in the 'super' call, and then try running your code. If that call blows up, take it out. That's really brittle, though, since who knows if NSFoobarControl will change in 10.6 to have an -awakeFromNib method? If it does, suddenly my app might crash on 10.6, and that might be really hard to track down.

The only reasonable solution seems to be to do exactly what I said — check whether the superclass implements the method before calling it.

—Jens

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden

References: 
 >super respondsToSelector (From: Russ McBride <email@hidden>)
 >Re: super respondsToSelector (From: Jens Alfke <email@hidden>)
 >Re: super respondsToSelector (From: Bill Bumgarner <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.