博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS进阶:Objective-C runtime(一)
阅读量:7045 次
发布时间:2019-06-28

本文共 2957 字,大约阅读时间需要 9 分钟。

  第一次看到runtime时,觉得太高大上,动态获取方法、属性等简直厉害的不要不要的。在经过查找资料+实践后,发现runtime并没有想象中那么复杂,接下来对runtime进行基本的介绍。

  要使用运行时方法需要引入runtime.h文件

  一、基础知识  

  Method :成员方法

  Ivar : 成员变量

  二、常用方法

  class_copyPropertyList : 获取属性列表

  class_copyMethodList : 获取成员方法列表

  class_copyIvarList:获取成员变量列表

  ivar_getName:获取变量名

  property_getName:获取属性名

 

  使用示例:

  1.获取成员变量列表

 

//1.获取变量list        unsigned int ivarCount = 0; //成员变量数        Ivar *ivarList = class_copyIvarList([self class], &ivarCount);//ivar数组                for (int i = 0; i < ivarCount; i++) {
//遍历 Ivar ivar = ivarList[i]; //获取ivar const char *name = ivar_getName(ivar);//获取变量名 NSString *key = [NSString stringWithUTF8String:name]; NSLog(@"%@", key); }       free(ivarList);
 

  2.获取属性列表

 

unsigned int count = 0;    objc_property_t *propertList = class_copyPropertyList([self class], &count);    for (int i = 0; i < count; i++) {        objc_property_t property = propertList[i];        const char *name = property_getName(property);        const char *attrs = property_getAttributes(property);//        property_copyAttributeValue(,) 第一个参数为objc_property_t,第二个参数"V"获取变量名,"T"获取类型        const char *value = property_copyAttributeValue(property, "V");        NSLog(@"name = %s, attrs = %s, value = %s", name, attrs, value);    }

    free(propertList);

  3.获取方法列表 

 

unsigned int count = 0;    Method *methodList = class_copyMethodList([self class], &count);    for (int i = 0 ; i < count; i++) {        Method method = methodList[i];        SEL selector = method_getName(method);//方法入口        const char *sel_name = sel_getName(selector);        NSLog(@"方法名 %s", sel_name);    }    free(methodList);

 

  三、使用方向:归档、字典<---->模型、框架封装等

  实现归档

  

#define WKCodingImplementing \- (void)encodeWithCoder:(NSCoder *)aCoder \{ \    unsigned int ivarCount = 0; \    Ivar *ivarList = class_copyIvarList([self class], &ivarCount); \    for (int i = 0; i < ivarCount; i++) { \        Ivar ivar = ivarList[i]; \        const char *name = ivar_getName(ivar); \        const char *type = ivar_getTypeEncoding(ivar); \        NSLog(@"%s-----%s", name, type); \        NSString *key = [NSString stringWithUTF8String:name]; \        id value = [self valueForKey:key]; \        [aCoder encodeObject:value forKey:key]; \    } \    free(ivarList); \} \- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder \{ \    if (self = [super init]) { \        unsigned int ivarCount = 0; \        Ivar *ivarList = class_copyIvarList([self class], &ivarCount); \        for (int i = 0; i < ivarCount; i++) { \            Ivar ivar = ivarList[i]; \            const char *name = ivar_getName(ivar); \            NSString *key = [NSString stringWithUTF8String:name]; \            NSLog(@"%@ %@", key, value); \            id value = [aDecoder decodeObjectForKey:key]; \            [self setValue:value forKey:key]; \        } \    } \    return self; \}

 

 

 

  

转载于:https://www.cnblogs.com/pretty-guy/p/4860265.html

你可能感兴趣的文章
(二)WebRTC手记之框架与接口
查看>>
hbase集群 常用维护命令
查看>>
deepinmind(转)
查看>>
滴滴顺风车设计总结(原创文章)
查看>>
android 10 事件
查看>>
练习10.9-2
查看>>
变化的区间树状数组,单点查询
查看>>
lastIndexOf方法——获取字符最后的索引
查看>>
一种文件捆绑型病毒研究
查看>>
二进制中1的个数
查看>>
opencv查找轮廓---cvFindContours && cvDrawCountours 用法及例子
查看>>
C# 之 服务端获取远程资源
查看>>
《大话操作系统——扎实project实践派》(8.2)(除了指令集.完)
查看>>
SAP 物料移动类型查询表
查看>>
Unity UGUI——Rect Transform包(Anchors)
查看>>
SNMP 实战1
查看>>
ZooKeeper概述(转)
查看>>
[nodejs] nodejs开发个人博客(一)准备工作
查看>>
Android仿微信界面--使用Fragment实现(慕课网笔记)
查看>>
泪奔在最后时刻
查看>>