Skip to content

Instantly share code, notes, and snippets.

@codetalks-new
Last active March 17, 2016 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codetalks-new/d23f6fdcf4135589a508 to your computer and use it in GitHub Desktop.
Save codetalks-new/d23f6fdcf4135589a508 to your computer and use it in GitHub Desktop.
Using Objective Runtime API Dump Class hierachy
#import <Foundation/Foundation.h>
@import ObjectiveC;
void print_class_hierachy(Class cls){
printf("%s\n",class_getName(cls));
Class superclass = class_getSuperclass(cls);
int current_level = 0;
while (superclass) {
current_level++;
for (int level = 0; level < current_level;level++) {
if(level + 1 == current_level){
printf("|----");
}else{
printf(" ");
}
}
printf("%s\n",class_getName(superclass));
superclass = class_getSuperclass(superclass);
}
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
static unsigned int classCount = 0;
Class * classList = objc_copyClassList(&classCount);
int level = 0;
int min_print_level = 4;
for (unsigned int i = 0; i < classCount; i++) {
Class leafClass = classList[i];
Class superclass = class_getSuperclass(leafClass);
level = 0;
while (superclass) {
level++;
superclass = class_getSuperclass(superclass);
}
if(level >= min_print_level){
print_class_hierachy(leafClass);
}
}
free(classList);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment