Hi I'm using this answer to get list of class to automatically build some structure.
My code looks like that:
NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity: 32];
Class* classes = NULL;
int numClasses = objc_getClassList(NULL, 0);
if (numClasses > 0 ) {
classes = (Class*)malloc(sizeof(Class) * numClasses);
numClasses = objc_getClassList(classes, numClasses);
for (int index = 0; index < numClasses; index++) {
Class nextClass = classes[index];
if ([nextClass isSubclassOfClass: BaseCmd.class] &&
![BaseCmd isSubclassOfClass: nextClass]) {
BaseCmd *cmd = [nextClass new];
result[cmd.name] = cmd;
}
}
free(classes);
}
return result;
So modification from answer is quite simple. It does work quite nicely until __NSAtom
class is processed (index = 1548
numClasses = 7628
so it is not out of bounds problem).
When this __NSAtom
class is reached there is a crash. Logs show:
*** NSForwarding: warning: object 0x7fff74978938 of class '__NSAtom' does not implement methodSignatureForSelector: -- trouble ahead
*** NSForwarding: warning: object 0x7fff74978938 of class '__NSAtom' does not implement doesNotRecognizeSelector: -- abort
Crash comes from condition isSubclassOfClass
.
Why it doesn't work? How can I fix it?
I'm using OS X Yosemite.
+[NSObject isSubclassOfClass:]
is a class method forNSObject
and not all classes are subclasses ofNSObject
.It seems as if you have find private class that is not a subclass of
NSObject
, so it requires a more delicate handling for checking for inheritance. Try: