美文网首页iOS Developer
Runtime 获取一个类的所有的属性、成员变量、方法

Runtime 获取一个类的所有的属性、成员变量、方法

作者: IAMCJ | 来源:发表于2017-06-16 14:50 被阅读98次

    这篇文章是写的一些关于 Runtime 的用法总结,主要的是一个引导作用,如果想要在项目中去使用,还需要对具体项目做出不同的处理,但是思路都是差不多的。

    本篇文章算是一个入门,获取一个类的所有的属性、成员变量、方法

    1.首先创建一个继承于NSObject的类:Coder,然后我们给他一些属性,成员变量,方法等:

    //Coder.h
    #import <Foundation/Foundation.h>
    
    @interfaceCoder :NSObject
    {
      NSString * _address;
      NSString * _code;
    }
    
    @property(nonatomic,copy) NSString * name;
    @property(nonatomic,assign) NSInteger age;
    
    /**
    获取所有的属性
    */
    - (NSDictionary *)getProperties;
    
    /**
    获取所有的成员变量
    */
    - (NSDictionary *)getIvars;
    
    /**
    获取所有的方法
    */
    - (NSDictionary *)getMethods;
    
    @end
    
    

    2.做完这些,我们去实现它的各个方法,上代码:

    //Coder.m
    #import "Coder.h"
    #import <objc/runtime.h>
    
    @implementation Coder
    
    // 获取所有的属性
    - (NSDictionary *)getProperties {
        
        unsigned int count = 0;
        
        // 获取类的所有 Property,如果没有属性 count 就为0
        objc_property_t * properties = class_copyPropertyList([self class], &count);
        NSMutableDictionary * dict = [NSMutableDictionary dictionary];
        
        for (NSUInteger i = 0; i < count; i ++) {
            
            // 获取属性的 Name 和 value
            const char * propertyName = property_getName(properties[i]);
            NSString * name = [NSString stringWithUTF8String:propertyName];
            id propertyValue = [self valueForKey:name];
            
            if (propertyValue) {
                dict[name] = propertyValue;
            } else {
                dict[name] = @"value为nil";
            }
        }
        // properties是一个数组指针,需要使用free函数来释放内存。
        free(properties);
        
        return dict;
    }
    
    // 获取所有的成员变量
    - (NSDictionary *)getIvars {
        unsigned int count = 0;
        
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        
        // 获取类的所有 Ivar
        Ivar *ivars = class_copyIvarList([self class], &count);
        
        for (NSUInteger i = 0; i < count; i ++) {
            // 获取成员变量的 Name 和 value
            const char *varName = ivar_getName(ivars[i]);
            NSString *name = [NSString stringWithUTF8String:varName];
            id varValue = [self valueForKey:name];
            
            if (varValue) {
                dict[name] = varValue;
            } else {
                dict[name] = @"value为nil";
            }
            
        }
        
        free(ivars);
        
        return dict;
    }
    
    // 获取所有的方法
    - (NSDictionary *)getMethods {
        unsigned int count = 0;
        
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        
        // 获取类的所有 Method
        Method *methods = class_copyMethodList([self class], &count);
        
        for (NSUInteger i = 0; i < count; i ++) {
            
            // 获取方法 Name
            SEL methodSEL = method_getName(methods[i]);
            const char *methodName = sel_getName(methodSEL);
            NSString *name = [NSString stringWithUTF8String:methodName];
            
            // 获取方法的参数列表
            int arguments = method_getNumberOfArguments(methods[i]);
            //因为消息发送的时候会有两个默认的参数(消息接受者和方法名),所以需要减去2
            dict[name] = @(arguments-2);
        }
        
        free(methods);
        
        return dict;
    }
    

    3.接下来,就可以去一个一个的去获得我们想要的了:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        Coder * coder = [[Coder alloc]init];
        coder.name = @"IAMCJ";
        coder.age = 18;
        [coder setValue:@"北京" forKey:@"_address"];//_address/address均可
        
        // 获取所有的属性
        NSDictionary *propertyDict = [coder getProperties];
        for (NSString *propertyName in propertyDict.allKeys) {
            NSLog(@"propertyName:%@, propertyValue:%@",propertyName, propertyDict[propertyName]);
        }
        
        // 获取所有的成员变量
        NSDictionary *ivarDict = [coder getIvars];
        for (NSString *ivarName in ivarDict.allKeys) {
            NSLog(@"ivarName:%@, ivarValue:%@",ivarName, ivarDict[ivarName]);
        }
        
        // 获取所有的方法
        NSDictionary *methodDict = [coder getMethods];
        for (NSString *methodName in methodDict.allKeys) {
            NSLog(@"methodName:%@, argumentsCount:%@", methodName, methodDict[methodName]);
        }
    }
    

    4.这是打印出来的内容:

    2017-06-16 11:34:51.414 GetPropertyIvarMethod[2853:49972] propertyName:name, propertyValue:IAMCJ
    2017-06-16 11:34:51.414 GetPropertyIvarMethod[2853:49972] propertyName:age, propertyValue:18
    2017-06-16 11:34:51.415 GetPropertyIvarMethod[2853:49972] ivarName:_name, ivarValue:IAMCJ
    2017-06-16 11:34:51.415 GetPropertyIvarMethod[2853:49972] ivarName:_age, ivarValue:18
    2017-06-16 11:34:51.415 GetPropertyIvarMethod[2853:49972] ivarName:_address, ivarValue:北京
    2017-06-16 11:34:51.415 GetPropertyIvarMethod[2853:49972] ivarName:_code, ivarValue:value为nil
    2017-06-16 11:34:51.415 GetPropertyIvarMethod[2853:49972] methodName:setName:, argumentsCount:1
    2017-06-16 11:34:51.416 GetPropertyIvarMethod[2853:49972] methodName:age, argumentsCount:0
    2017-06-16 11:34:51.416 GetPropertyIvarMethod[2853:49972] methodName:getIvars, argumentsCount:0
    2017-06-16 11:34:51.416 GetPropertyIvarMethod[2853:49972] methodName:getMethods, argumentsCount:0
    2017-06-16 11:34:51.416 GetPropertyIvarMethod[2853:49972] methodName:setAge:, argumentsCount:1
    2017-06-16 11:34:51.416 GetPropertyIvarMethod[2853:49972] methodName:getProperties, argumentsCount:0
    2017-06-16 11:34:51.416 GetPropertyIvarMethod[2853:49972] methodName:.cxx_destruct, argumentsCount:0
    2017-06-16 11:34:51.417 GetPropertyIvarMethod[2853:49972] methodName:name, argumentsCount:0
    

    从上面我们可以看到成员变量的个数比属性多两个,其实这就是
    class_copyPropertyList 和 class_copyIvarList 的区别,前者是获取所有的属性列表,后者是获取所有的成员变量列表,如果你想问 Coder 这个类的属性有两个,成员变量不也是两个吗?其实每个属性会自动生成一个 _XXX 的成员变量,并自动生成 setter 和 getter 方法,从上面的打印内容你可以清晰的看出来。
    注:方法列表里面多了一个 .cxx_destruct 方法,你可能不知道这个方法是干什么的,其实 .cxx_destruct 方法原本是为了 C++ 对象析构的,在这里 ARC 借用了这个方法插入代码实现了自动内存释放的工作。想深入了解的同学点这里.

    这里的内容是为了让你去理解这个东西,但是你可能还不知道可以用它干什么,想了解一下可以用这个做一些什么事情,看一下iOS利用Runtime自定义控制器POP手势动画,如果在此之前内没用过此类方法解决问题,看完之后你会原来还可以这样,666。

    我会再写几篇关于Runtime的用法的文章,同时也在提升自己的能力,项目比较忙,所以更新时间不定,水平有限,有错误望指出。Demo在这里,不麻烦的话 star 一下,感谢大家。

    相关文章

      网友评论

        本文标题:Runtime 获取一个类的所有的属性、成员变量、方法

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