获取所有本机类的列表 [英] Get List of all native classes
问题描述
我想获得我已加载到iOS项目中的所有本机类(NSString,NSNumber,int,float,NSSet,NSDictionary)..
I want to get all the native classes (NSString, NSNumber, int, float, NSSet, NSDictionary) that I have loaded into my iOS Project..
ie ,如果我创建了一个名为TestClass的自定义类,我不希望它列出...
i.e., if I have created a custom class named "TestClass" I don't want it listed...
我已经有了一个代码,但它返回了所有类的名称以任何方式加载我可以修改代码只将列表限制为Native类?
I have already got a code but it returns names of all classes loaded any way I can modify the code to limit the list to Native classes only?
#import <objc/runtime.h>
#import <dlfcn.h>
#import <mach-o/ldsyms.h>
unsigned int count;
const char **classes;
Dl_info info;
dladdr(&_mh_execute_header, &info);
classes = objc_copyClassNamesForImage(info.dli_fname, &count);
for (int i = 0; i < count; i++) {
NSLog(@"Class name: %s", classes[i]);
Class class = NSClassFromString ([NSString stringWithCString:classes[i] encoding:NSUTF8StringEncoding]);
// Do something with class
}
推荐答案
你会得到所有加载的类
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];
NSLog(@"%s", class_getName(c));
}
free(classes);
}
(代码来自 objc_getClassList 文档。)
要限制列表,您可以查看加载类的包,例如
To restrict the list, you can check the bundle from which the class was loaded, e.g.
Class c = classes[i];
NSBundle *b = [NSBundle bundleForClass:c];
if (b != [NSBundle mainBundle])
...
对于未从您的应用程序加载的所有类。
for all classes that are not loaded from your application.
这篇关于获取所有本机类的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!