美文网首页iOS专题资源__系统知识点iOS相关iOS 进阶
三分钟教会你runtime获取属性和成员变量

三分钟教会你runtime获取属性和成员变量

作者: CoderDancer | 来源:发表于2016-06-17 16:33 被阅读6667次

    runtime之属性和成员变量

    目录

    • 成员变量和属性到底是什么?
    • 怎么通过runtime获取属性?
    • 怎么通过runtime获取成员变量?
    • 成员变量和属性的区别?
    • 实际应用场景是什么?

    成员变量

    1、成员变量的定义

    Ivar: 实例变量类型,是一个指向objc_ivar结构体的指针
    typedef struct objc_ivar *Ivar;

    2、相关函数

    // 获取所有成员变量
    class_copyIvarList
    // 获取成员变量名
    ivar_getName
    // 获取成员变量类型编码
    ivar_getTypeEncoding
    // 获取指定名称的成员变量
    class_getInstanceVariable
    // 获取某个对象成员变量的值
    object_getIvar
    // 设置某个对象成员变量的值
    object_setIvar

    说明:
    property_getAttributes函数返回objc_property_attribute_t结构体列表,objc_property_attribute_t结构体包含namevalue,常用的属性如下:

    属性类型 name值:T value:变化
    编码类型 name值:C(copy) &(strong) W(weak)空(assign) 等 value:
    非/原子性 name值:空(atomic) N(Nonatomic) value:
    变量名称 name值:V value:变化

    使用property_getAttributes获得的描述是property_copyAttributeList能获取到的所有的namevalue的总体描述,如 T@"NSDictionary",C,N,V_dict1

    3、实例应用

    <!--Person.h文件-->
    @interface Person : NSObject
    {
        NSString *address;
    }
    @property(nonatomic,strong)NSString *name;
    @property(nonatomic,assign)NSInteger age;
    

    //遍历获取Person类所有的成员变量IvarList
    - (void) getAllIvarList {
        unsigned int methodCount = 0;
        Ivar * ivars = class_copyIvarList([Person class], &methodCount);
        for (unsigned int i = 0; i < methodCount; i ++) {
            Ivar ivar = ivars[i];
            const char * name = ivar_getName(ivar);
            const char * type = ivar_getTypeEncoding(ivar);
            NSLog(@"Person拥有的成员变量的类型为%s,名字为 %s ",type, name);
        }
        free(ivars);
    }
    
    

    <!--打印结果-->
    2016-06-15 20:26:39.412 demo-Cocoa之method swizzle[17798:2565569] Person拥有的成员变量的类型为@"NSString",名字为 address 
    2016-06-15 20:26:39.413 demo-Cocoa之method swizzle[17798:2565569] Person拥有的成员变量的类型为@"NSString",名字为 _name 
    2016-06-15 20:26:39.413 demo-Cocoa之method swizzle[17798:2565569] Person拥有的成员变量的类型为q,名字为 _age 
    

    属性

    1、属性的定义

    objc_property_t:声明的属性的类型,是一个指向objc_property结构体的指针
    typedef struct objc_property *objc_property_t;

    2、相关函数

    // 获取所有属性
    class_copyPropertyList
    说明:使用class_copyPropertyList并不会获取无@property声明的成员变量
    // 获取属性名
    property_getName
    // 获取属性特性描述字符串
    property_getAttributes
    // 获取所有属性特性
    property_copyAttributeList

    3、实例应用

    <!--Person.h文件-->
    @interface Person : NSObject
    {
        NSString *address;
    }
    @property(nonatomic,strong)NSString *name;
    @property(nonatomic,assign)NSInteger age;
    

    //遍历获取所有属性Property
    - (void) getAllProperty {
        unsigned int propertyCount = 0;
        objc_property_t *propertyList = class_copyPropertyList([Person class], &propertyCount);
        for (unsigned int i = 0; i < propertyCount; i++ ) {
            objc_property_t *thisProperty = propertyList[i];
            const char* propertyName = property_getName(*thisProperty);
            NSLog(@"Person拥有的属性为: '%s'", propertyName);
        }
    }
    

    <!--打印结果-->
    2016-06-15 20:25:19.653 demo-Cocoa之method swizzle[17778:2564081] Person拥有的属性为: 'name'
    2016-06-15 20:25:19.653 demo-Cocoa之method swizzle[17778:2564081] Person拥有的属性为: 'age'
    

    应用具体场景

    1、Json到Model的转化

    在开发中相信最常用的就是接口数据需要转化成Model了(当然如果你是直接从Dict取值的话。。。),很多开发者也都使用著名的第三方库如JsonModelMantleMJExtension等,如果只用而不知其所以然,那真和“搬砖”没啥区别了,下面我们使用runtime去解析json来给Model赋值。

    原理描述:用runtime提供的函数遍历Model自身所有属性,如果属性在json中有对应的值,则将其赋值。

    核心方法:在NSObject的分类中添加方法:

    - (instancetype)initWithDict:(NSDictionary *)dict {
     
        if (self = [self init]) {
            //(1)获取类的属性及属性对应的类型
            NSMutableArray * keys = [NSMutableArray array];
            NSMutableArray * attributes = [NSMutableArray array];
            /*
             * 例子
             * name = value3 attribute = T@"NSString",C,N,V_value3
             * name = value4 attribute = T^i,N,V_value4
             */
            unsigned int outCount;
            objc_property_t * properties = class_copyPropertyList([self class], &outCount);
            for (int i = 0; i < outCount; i ++) {
                objc_property_t property = properties[i];
                //通过property_getName函数获得属性的名字
                NSString * propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
                [keys addObject:propertyName];
                //通过property_getAttributes函数可以获得属性的名字和@encode编码
                NSString * propertyAttribute = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];
                [attributes addObject:propertyAttribute];
            }
            //立即释放properties指向的内存
            free(properties);
     
            //(2)根据类型给属性赋值
            for (NSString * key in keys) {
                if ([dict valueForKey:key] == nil) continue;
                [self setValue:[dict valueForKey:key] forKey:key];
            }
        }
        return self;
     
    }
    
    

    读者可以进一步思考:

    如何识别基本数据类型的属性并处理
    空(nil,null)值的处理
    json中嵌套json(Dict或Array)的处理

    尝试解决以上问题,你也能写出属于自己的功能完备的Json转Model库。

    2、快速归档

    有时候我们要对一些信息进行归档,如用户信息类UserInfo,这将需要重写initWithCoderencodeWithCoder方法,并对每个属性进行encodedecode操作。那么问题来了:当属性只有几个的时候可以轻松写完,如果有几十个属性呢?那不得写到天荒地老.

    原理描述:用runtime提供的函数遍历Model自身所有属性,并对属性进行encodedecode操作。

    核心方法:在Model的基类中重写方法:

    - (id)initWithCoder:(NSCoder *)aDecoder {
        if (self = [super init]) {
            unsigned int outCount;
            Ivar * ivars = class_copyIvarList([self class], &outCount);
            for (int i = 0; i < outCount; i ++) {
                Ivar ivar = ivars[i];
                NSString * key = [NSString stringWithUTF8String:ivar_getName(ivar)];
                [self setValue:[aDecoder decodeObjectForKey:key] forKey:key];
            }
        }
        return self;
    }
    
    - (void)encodeWithCoder:(NSCoder *)aCoder {
        unsigned int outCount;
        Ivar * ivars = class_copyIvarList([self class], &outCount);
        for (int i = 0; i < outCount; i ++) {
            Ivar ivar = ivars[i];
            NSString * key = [NSString stringWithUTF8String:ivar_getName(ivar)];
            [aCoder encodeObject:[self valueForKey:key] forKey:key];
        }
    }
    
    

    3、访问私有变量

    我们知道,OC中没有真正意义上的私有变量和方法,要让成员变量私有,要放在m文件中声明,不对外暴露。如果我们知道这个成员变量的名称,可以通过runtime获取成员变量,再通过getIvar来获取它的值。

    方法:

    Ivar ivar = class_getInstanceVariable([Model class], "_str1");
    NSString * str1 = object_getIvar(model, ivar);
    
    

    写给看客

    对于已入行的程序员来说,刨根问底,挖开底层是突破瓶颈的必经之路。要想要从技术开发的普通工人变成真正的工程师,就必须需要啃下这块骨头。
    而且在完成这篇文章的过程中,我发现自己之前走了不少弯路。因为底层理解不够,在扩展学习时深感效率低下,过目即忘。归根结底是只了解皮毛,无法内化,深入理解开发者的思路。
    当然文章也多是个人理解,如有错误也请留言指正,共同成长。

    写在最后

    我得写作原则:
    在技术学习道路上,阅读量和代码量绝不能线性提升你的技术水平。
    同样写文章也是如此,作者所写的文章完全是基于自己对技术的理解,在写作时也力求形象不抽象。绝不copy充数,所以也欢迎大家关注和参与讨论。
    技术学习绝不能孤胆英雄独闯天涯,而应在一群人的交流碰撞,享受智慧火花的狂欢。
    希望我的文章能成为你的盛宴,也渴望你的建议能成为我的大餐。
    如有错误请留言指正,对文章感兴趣可以关注作者不定期更新。

    相关文章

      网友评论

      • YY程序猿:有些通用属性,比如hidesBottomBarWhenPushed,能否获取到呢
        CoderDancer:@夏日的忧殇 之前链接了一个文件,忘记移除了,现在OK了
        夏日的忧殇:连接下载 运行报错
        CoderDancer:可以获取到,我曾经写了个工具类,可以获取所有属性变量,成员变量,类,继承链,协议,实例方法和类方法,贴出个链接,你你参考下。
        https://github.com/walkertop/OCDeepLearning里,有个工具类,叫做GBRuntimeLog
      • 0x7472616d70:写得很好,不错
      • 7996b6afa902:文章写得好极了~谢谢分享~!!
      • 有心向往:急需这样的理解,感谢分享

      本文标题:三分钟教会你runtime获取属性和成员变量

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