iOS 获取类所有子类
#include <objc/runtime.h>
+ (NSArray *)findAllOf:(Class)defaultClass
{
int count = objc_getClassList(NULL, 0);
if (count <= 0)
{
@throw@"Couldn't retrieve Obj-C class-list";
return [NSArray arrayWithObject:defaultClass];
}
NSMutableArray *output = [NSMutableArray arrayWithObject:defaultClass];
Class *classes = (Class *) malloc(sizeof(Class) * count);
objc_getClassList(classes, count);
for (int i = 0; i < count; ++i) {
if (defaultClass == class_getSuperclass(classes[i]))//子类
{
[output addObject:classes[i]];
}
}
free(classes);
return [NSArray arrayWithArray:output];
}
用法 NSArray *arrayChildClass = [self findAllOf: [UIView Class] ] ;//获取UIView的所以子类