在未公开的情况下访问父类中的方法 [英] Accessing a method in a super class when it's not exposed
问题描述
在子类中,我重写了超类中未公开的方法.我知道我具有正确的签名,因为它已成功覆盖超类实现.但是,作为新实现的一部分,我需要从子类的实现中调用超类的实现.
In a subclass, I'm overriding a method that is not exposed in the super class. I know that I have the correct signature as it is successfully overriding the superclass implementation. However, as part of the the new implementation, I need to call the superclass's implementation from the subclass's implementation.
因为它没有公开,所以我必须通过调用performSelector
来调用该方法:
Because it's not exposed I have to invoke the method via a call to performSelector
:
SEL superClassSelector = NSSelectorFromString(@"methodToInvoke");
[super performSelector:superClassSelector];
但是,在我的应用程序中,这会导致无限递归循环,每次我尝试调用超类的实现时都会调用子类的实现.
However, in my application this results in an infinite recursive loop where the subclass's implementation is invoked every time I try to invoke the superclass's implementation.
有什么想法吗?
我意识到这是一种不典型的情况,但是不幸的是,没有办法解决我正在尝试做的事情.
I realize this is an atypical situation but unfortunately there's no way to get around what I'm trying to do.
推荐答案
我处理此问题的方法是使用要从子类调用的方法在子类实现文件中重新声明您的超类接口.
The way I've dealt with this is to re-declare your super class' interface in your subclass implementation file with the method you want to call from the subclass
@interface MySuperclass()
- (void)superMethodIWantToCall;
@end
@implementation MySubclass
- (void)whateverFunction {
//now call super method here
[super superMethodIWantToCall];
}
@end
我不确定这是否是最好的做事方法,但是它很简单并且对我有用!
I'm not sure if this is the best way to do things but it is simple and works for me!
这篇关于在未公开的情况下访问父类中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!