ClassGetSubclasses
#import "ViewController.h"
#import <objc/objc-runtime.h>
NSArray *ClassGetSubclasses(Class parentClass) {
int numClasses = objc_getClassList(NULL, 0);
Class *classes = NULL;
classes = malloc(sizeof(Class) * numClasses);
numClasses = objc_getClassList(classes, numClasses);
NSMutableArray *result = [NSMutableArray array];
for (NSInteger i = 0; i < numClasses; i++) {
Class superClass = classes[i];
do{
superClass = class_getSuperclass(superClass);
} while(superClass && superClass != parentClass);
if (superClass == nil) {
continue;
}
[result addObject:classes[i]];
}
free(classes);
return result;
}
注意:objc_getClassList()方法的CPU开支非常大,此方法尽量少用。
网友评论