iOS - Runtime 获取类的详细信息

iOS - Runtime 获取类的详细信息

IOS小彩虹2021-08-23 11:28:25200A+A-
  1. 获取类的属性列表
    unsigned int count;
    objc_property_t *propertyList = class_copyPropertyList(self.class, &count);
    for (unsigned int i = 0; i < count; i++) {
        const char *propertyName = property_getName(propertyList[i]);
        NSLog(@"propertyname; %@", [NSString stringWithUTF8String:propertyName]);
    }
  1. 获取所有成员变量
    unsigned int count;
    Ivar *ivarList = class_copyIvarList(self.class, &count);
    for (int i = 0; i < count; i ++) {
        Ivar ivar = ivarList[i];
        const char *ivarName = ivar_getName(ivar);
        NSLog(@"ivarName: %@", [NSString stringWithUTF8String: ivarName]);
    }
  1. 获取所有方法
    unsigned int count;
    Method *methodList = class_copyMethodList(self.class, &count);
    for (int i = 0; i < count; i ++) {
        Method method = methodList[i];
        SEL methodName = method_getName(method);
        NSLog(@"methodName: %@", NSStringFromSelector(methodName));
    }
  1. 获取当前所遵循的所有协议
    unsigned int count;
    __unsafe_unretained Protocol **protocolList = class_copyProtocolList([self class], &count);
    for (int i = 0; i < count; i ++) {
        Protocol *protocol = protocolList[i];
        const char *protocolName = protocol_getName(protocol);
        NSLog(@"protocolName: %@", [NSString stringWithUTF8String:protocolName]);
    }

点击这里复制本文地址 以上内容由权冠洲的博客整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!

支持Ctrl+Enter提交

联系我们| 本站介绍| 留言建议 | 交换友链 | 域名展示
本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除

权冠洲的博客 © All Rights Reserved.  Copyright quanguanzhou.top All Rights Reserved
苏公网安备 32030302000848号   苏ICP备20033101号-1
本网站由 提供CDN/云存储服务

联系我们