Check if subclass overrides a method

前端 未结 7 2471
耶瑟儿~
耶瑟儿~ 2021-02-09 13:15

Is it possible to check whether a subclass implements a method that exists either in its immediate superclass or in some superclass of its superclass, etc?

E.g. I subcla

7条回答
  •  一生所求
    2021-02-09 13:57

    Unfortunately, there is no equivalent to respondsToSelector: that will do precisely this job for you. As you will probably know, respondsToSelector: works in the way that it will return YES as long as the class itself or any of its superclasses implements this method.

    But why not just put an empty implementation of the method into your custom subclass, that way you make sure that calling it doesn't have any effect and doesn't call the same method in the superclass. Did you think about this?

    Update: While there is no method equivalent to respondsToSelector:, you might want to take a look at the Objective-C Runtime. It's a library that allows you to inspect characteristics of a class during runtime (a bit similar to Java reflections). Check out the reference here.

提交回复
热议问题