Using objc_getClassList under arc

2019-02-03 04:30发布

Has anybody managed to use objc_getClassList under arc, short of turning arc off for the file in question?

The fundamental problem is that one of the parameters is a C array of Class pointers.

1条回答
爷的心禁止访问
2楼-- · 2019-02-03 04:39

This code should work under ARC:

int numClasses;
Class *classes = NULL;

classes = NULL;
numClasses = objc_getClassList(NULL, 0);
NSLog(@"Number of classes: %d", numClasses);

if (numClasses > 0 )
{
    classes = (__unsafe_unretained Class *)malloc(sizeof(Class) * numClasses);
    numClasses = objc_getClassList(classes, numClasses);
    for (int i = 0; i < numClasses; i++) {
        NSLog(@"Class name: %s", class_getName(classes[i]));
    }
    free(classes);
}
查看更多
登录 后发表回答