Dynamically find available Objective-C classes
objc_getClassList give you a list of all available classes by asking the ObjC runtime.
Don’t forget to import to runtime (#import <objc/runtime.h>) and link to it (libobjc.dylib).
You can then do funny things; for example: finding classes with a name matching a specific pattern with a block:
NSArray* getClassesMatching(BOOL(^block)(NSString *className)) {
NSMutableArray *array = [NSMutableArray array];
int numClasses;
Class * classes = NULL;
classes = NULL;
numClasses = objc_getClassList(NULL, 0);
if (numClasses > 0 ) {
classes = malloc(sizeof(Class) * numClasses);
numClasses = objc_getClassList(classes, numClasses);
Class class = nil;
for (int i = 0; i < numClasses; i++) {
class = classes[i];
if (block([NSString stringWithCString:class_getName(classes[i]) encoding:NSUTF8StringEncoding])) {
[array addObject:class];
}
}
free(classes);
}
return [array copy];
}
Written by Arnaud Coomans
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ios
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#