Skip to content

Instantly share code, notes, and snippets.

@tyrone-sudeium
Last active January 25, 2017 04:49
Show Gist options
  • Save tyrone-sudeium/bd69d1e68238bdb168adc8b07ead8ae4 to your computer and use it in GitHub Desktop.
Save tyrone-sudeium/bd69d1e68238bdb168adc8b07ead8ae4 to your computer and use it in GitHub Desktop.
Gets every method in the current objc runtime and orders them in a CSV by length
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
int main(int argc, char *argv[])
{
int numberOfClasses = objc_getClassList(NULL, 0);
Class *classes = NULL;
classes = (__unsafe_unretained Class *)malloc(numberOfClasses * sizeof(Class));
numberOfClasses = objc_getClassList(classes, numberOfClasses);
NSMutableDictionary *methodsByLength = [NSMutableDictionary new];
for (int i = 0; i < numberOfClasses; i++) {
Class class = classes[i];
unsigned int numberOfMethods = 0;
Method *instanceMethods = class_copyMethodList(class, &numberOfMethods);
for (int j = 0; j < numberOfMethods; j++) {
Method method = instanceMethods[j];
SEL selector = method_getName(method);
NSString *methodName = NSStringFromSelector(selector);
NSUInteger length = [methodName length];
NSMutableArray *methods = methodsByLength[@(length)];
if (methods == nil) {
methods = [NSMutableArray new];
methodsByLength[@(length)] = methods;
}
[methods addObject: methodName];
}
Class metaClass = objc_getMetaClass([NSStringFromClass(class) cStringUsingEncoding:NSASCIIStringEncoding]);
Method *classMethods = class_copyMethodList(metaClass, &numberOfMethods);
for (int j = 0; j < numberOfMethods; j++) {
Method method = classMethods[j];
SEL selector = method_getName(method);
NSString *methodName = NSStringFromSelector(selector);
NSUInteger length = [methodName length];
NSMutableArray *methods = methodsByLength[@(length)];
if (methods == nil) {
methods = [NSMutableArray new];
methodsByLength[@(length)] = methods;
}
[methods addObject: methodName];
}
}
NSArray *keys = [[methodsByLength allKeys] sortedArrayUsingSelector: @selector(compare:)];
NSMutableString *csv = [NSMutableString new];
for (NSNumber *len in keys) {
NSArray *methods = methodsByLength[len];
[csv appendFormat: @"%@", len];
for (NSString *methodName in methods) {
[csv appendFormat: @",%@", methodName];
}
[csv appendString: @"\n"];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment