Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get list of all classes conform to certain protocol in Xcode 4?

I am new to Xcode. I want to know how can I know all classes conform to certain protocol in Xcode 4.3.1? And how can I know all subclasses of one class?

like image 312
icespace Avatar asked Mar 27 '12 03:03

icespace


2 Answers

Use the Objective-C runtime functions.

  • objc_getClassList to get the list of Classes
  • class_getSuperclass or the -superclass method to walk the superclass chain
  • class_conformsToProtocol or the -conformsToProtocol: method to check if a class conforms to a protocol
like image 122
Kurt Revis Avatar answered Nov 01 '22 23:11

Kurt Revis


Protocol *protocol = @protocol(YourProtocol);

int numberOfClasses = objc_getClassList(NULL, 0);
Class *classList = malloc(numberOfClasses * sizeof(Class));
numberOfClasses = objc_getClassList(classList, numberOfClasses);

for (int idx = 0; idx < numberOfClasses; idx++) 
{
    Class class = classList[idx];
    if (class_getClassMethod(class, @selector(conformsToProtocol:)) && [class conformsToProtocol:protocol])
    {
        NSLog(@"%@", NSStringFromClass(class));
    }
}
free(classList);
like image 26
Denis Mikhaylov Avatar answered Nov 02 '22 01:11

Denis Mikhaylov