Last active
March 17, 2016 04:40
-
-
Save codetalks-new/d23f6fdcf4135589a508 to your computer and use it in GitHub Desktop.
Using Objective Runtime API Dump Class hierachy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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