How do I get list of all classes conform to certain protocol in Xcode 4?
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);
Use the Objective-C runtime functions.
objc_getClassList
to get the list ofClass
esclass_getSuperclass
or the-superclass
method to walk the superclass chainclass_conformsToProtocol
or the-conformsToProtocol:
method to check if a class conforms to a protocol