如何确定超类是否也实现了我的子类实现的相同方法?

时间:2021-09-14 20:47:24

I want to know if the superclass implements method A from my subclass, which also implements method A, so that I can safely call [super A] from my subclass without getting an exception.

我想知道超类是否从我的子类实现了方法A,它也实现了方法A,这样我就可以安全地从子类调用[super A],而不会出现异常。

NSObject's respondsToSelector: doesn't work in this case, since that will always return true (because my subclass implements the method in question). Any ideas?

NSObject的respondsToSelector:在这种情况下不起作用,因为那将总是返回true(因为我的子类实现了有问题的方法)。什么好主意吗?

3 个解决方案

#1


12  

You can use the class method instancesRespondToSelector: to do this. So from the subclass you can call [[self superclass] instancesRespondToSelector:@selector(...)] to determine if the superclass implements the method you require.

可以使用类方法instancesRespondToSelector:这样做。因此,可以从子类调用[[self超类]instancesRespondToSelector:@selector(…)]来确定超类是否实现了所需的方法。

#2


2  

Calling [[self superclass] instancesRespondToSelector:@selector(...)] will not work, if the class that implements it is subclassed and the method is called on the instance of the subclassed-class. One solution is to explicitly type the name of the current class that implements it:

调用[[[self父类]instancesRespondToSelector:@selector(…)]将不起作用,如果实现它的类是子类,并且方法在子类的实例上被调用。一种解决方案是显式地键入实现它的当前类的名称:

[[ExplicitClassName superclass] instancesRespondToSelector:@selector(...)]

#3


0  

Try [[[self class] superclass] instancesRespondToSelector:@selector(. )]]]

尝试[[[[自我类]超类]instancesRespondToSelector:@selector(.)]]]

[super respondsToSelector:] invokes the superclass's implementation of respondsToSelector, which is likely the same as the current class's. It does not check if the superclass implements the method.

[super respondsToSelector:]调用超类的respondsToSelector实现,它可能与当前类的实现相同。它不检查超类是否实现该方法。

#1


12  

You can use the class method instancesRespondToSelector: to do this. So from the subclass you can call [[self superclass] instancesRespondToSelector:@selector(...)] to determine if the superclass implements the method you require.

可以使用类方法instancesRespondToSelector:这样做。因此,可以从子类调用[[self超类]instancesRespondToSelector:@selector(…)]来确定超类是否实现了所需的方法。

#2


2  

Calling [[self superclass] instancesRespondToSelector:@selector(...)] will not work, if the class that implements it is subclassed and the method is called on the instance of the subclassed-class. One solution is to explicitly type the name of the current class that implements it:

调用[[[self父类]instancesRespondToSelector:@selector(…)]将不起作用,如果实现它的类是子类,并且方法在子类的实例上被调用。一种解决方案是显式地键入实现它的当前类的名称:

[[ExplicitClassName superclass] instancesRespondToSelector:@selector(...)]

#3


0  

Try [[[self class] superclass] instancesRespondToSelector:@selector(. )]]]

尝试[[[[自我类]超类]instancesRespondToSelector:@selector(.)]]]

[super respondsToSelector:] invokes the superclass's implementation of respondsToSelector, which is likely the same as the current class's. It does not check if the superclass implements the method.

[super respondsToSelector:]调用超类的respondsToSelector实现,它可能与当前类的实现相同。它不检查超类是否实现该方法。