在iOS中如何获取一个类的所有子类

- (NSArray *)getAllSubClassNameWithClass:(Class)class {
    NSMutableArray *results = [NSMutableArray array];
    int numClasses;
    Class *classes = NULL;
    numClasses = objc_getClassList(NULL,0);
    if (numClasses > 0) {
        classes = (__unsafe_unretained Class *)malloc(sizeof(Class) * numClasses);
        numClasses = objc_getClassList(classes, numClasses);
        for (int i = 0; i < numClasses; i++) {
            if (class_getSuperclass(classes[i]) == class){
                [results addObject:NSStringFromClass(classes[i])];
                NSLog(@"%@\n", NSStringFromClass(classes[i]));
            }
        }
        free(classes);
    }
    return results;
}

你可能感兴趣的:(在iOS中如何获取一个类的所有子类)