OC 获取Xcode内所有的控制器类名方法
#import <objc/runtime.h>
#import <dlfcn.h>
#import <mach-o/ldsyms.h>
{
unsigned int count = 0;
const char **classes = nil;
Dl_info info;
dladdr(&_mh_execute_header, &info);
NSLog(@"%s", info.dli_fname);
/*
info.dli_fame 是工程编译时工程的路径
objc_copyClassNamesForImage 返回所有类的名称
*/
classes = objc_copyClassNamesForImage(info.dli_fname, &count);
for (int index = 0; index < count; ++index) {
NSString *className = [NSString stringWithCString:classes[index] encoding:NSUTF8StringEncoding];
if ([className containsString:@"ViewController"]) {
NSLog(@"className = %@", className);
}
}
}
附:class与string之间的互相转换
通过类名获取类
NSString *classStr = @"STImageView";
Class STImageView = NSClassFromString(classStr);
STImageView *imageView = [[STImageView alloc]init];
通过类获取类名字符串
NSString *classStr = NSStringFromClass([STImageViewclass]);