开发者

How do you call a method for an Objective-C object's superclass from elsewhere?

开发者 https://www.devze.com 2022-12-31 06:14 出处:网络
If you\'re implementing a subclass, you can, within your implementation, explicitly call the superclass\'s method, even if you\'ve overridden that method, i.e.:

If you're implementing a subclass, you can, within your implementation, explicitly call the superclass's method, even if you've overridden that method, i.e.:

[self overriddenMethod]; //calls the subclass's method
[super overriddenMethod]; //calls the superclass's method

What if you want to call the superclass's method from somewhere outside the subclass's implementation, i.e.:

[[object super] overriddenMethod]开发者_JS百科; //crashes

Is this even possible? And by extension, is it possible to go up more than one level within the implementation, i.e.:

[[super super] overriddenMethod]; //will this work?


A more pleasant option (for debugging) is to add a category method which does it for you:

- (void) callSuperMethod { [super overriddenMethod]; }

The fact that there’s no more convenient way to do this is very much by design.


You can send [[[object class] superclass] methodForSelector: ...] and invoke through the IMP pointer directly, if you want... but if you're doing this, there's probably something very wrong with your application's design.

0

精彩评论

暂无评论...
验证码 换一张
取 消