美文网首页
获取项目中所有 VC 控制器

获取项目中所有 VC 控制器

作者: 孙伟胜 | 来源:发表于2021-11-18 17:18 被阅读0次
    - (void)getAllClassName {
        unsigned int count;
        const char **classes;
        Dl_info info;
        
        void *_mh_execute_header = __builtin_return_address(0);
        
        //1.获取app的路径
        dladdr(_mh_execute_header, &info);
        
        //2.返回当前运行的app的所有类的名字,并传出个数
        //classes:二维数组 存放所有类的列表名称
        //count:所有的类的个数
        classes = objc_copyClassNamesForImage(info.dli_fname, &count);
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        for (int i = 0; i < count; i++) {
            //3.遍历并打印,转换Objective-C的字符串
            NSString *className = [NSString stringWithCString:classes[i] encoding:NSUTF8StringEncoding];
            if ([className containsString:@"ViewController"] || [className containsString:@"VC"]) {
                NSLog(@"__debug NSClassFromString = %@",NSClassFromString(className));
                NSLog(@"__getAllVC =========== %@", className);
                [dict setObject:className forKey:className];
            }
            //根据类名调用
        }
    }
    

    获取某个plist文件中所有的名称

    - (void)pagePlistAllVC{
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"ViewController" ofType:@"plist"];
        NSDictionary *dataDic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
        for (NSString *className in dataDic.allKeys) {
            NSLog(@"__allPageClass =========== %@", className);
        }
    }
    

    相关文章

      网友评论

          本文标题:获取项目中所有 VC 控制器

          本文链接:https://www.haomeiwen.com/subject/zefjtrtx.html