I am playing around with some reflection in Objective C (iOS app) and building a customized testing environment, and I am trying to get a list of classes that I have created in my project, so I can iterate over these and look for certain method declarations by using reflection.
My problem is to retrieve that list. I do not want to manually type in each class into a list or some static field in my reflection class, but instead let it be generic for any project.
The list could be a list of class names, or objects of the type Class
.
I did manage to find the objc_getClassList()
function in the documentation: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html
But this creates a list of ALL classes, and I wouldn't be able to determine which are user defined and which are not. I have tried to seek guidance around the Internet, but without luck finding an answer or similar question.
Any help is appreciated!
EDIT:
Now I also tried to use objc_copyImageNames()
in hope that I would be able to find my project as an image, and then use objc_copyClassNamesForImage()
to get the list, but with no luck as the project is not considered an image, Worth a shoot I guess.
You could use the code from the objc_getClassList()
documentation
and check each class with [NSBundle bundleForClass:class]
if it is defined in your
application:
int numClasses;
Class * classes = NULL;
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++) {
Class c = classes[i];
NSBundle *b = [NSBundle bundleForClass:c];
if (b == [NSBundle mainBundle]) {
NSLog(@"%s", class_getName(c));
// ...
}
}
free(classes);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With