Find Out Objective-C Class Overrides Method

How can I find out at runtime if a class overrides the method of its superclass?

For example, I want to find out if a class has its own implementation of isEqual: or hash instead of relying on a superclass.

+7
objective-c objective-c-runtime cocoa introspection
source share
1 answer

You just need to get a list of methods and find the one you need:

 #import <objc/runtime.h> BOOL hasMethod(Class cls, SEL sel) { unsigned int methodCount; Method *methods = class_copyMethodList(cls, &methodCount); BOOL result = NO; for (unsigned int i = 0; i < methodCount; ++i) { if (method_getName(methods[i]) == sel) { result = YES; break; } } free(methods); return result; } 

class_copyMethodList returns only methods that are defined directly in the class in question, and not in superclasses, so this should be what you mean.

If you need class methods, use class_copyMethodList(object_getClass(cls), &count) .

+4
source share

All Articles