iOS – Runtime 获取类的详细信息

  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]);
    }
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享