博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Runtime
阅读量:4499 次
发布时间:2019-06-08

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

开发中可能用到的Runtime:在Category中添加属性, 交换方法的实现,归档接档(获取所用的变量, 属性, 方法名, 协议名),动态添加方法

1:添加属性

objc_setAssociatedObject(self, _cmd, girlFriend, OBJC_ASSOCIATION_RETAIN);objc_getAssociatedObject(self, @selector(setGirlFriend:));

2:方法交换

+ (void)MethodSwapWithSEL:(SEL)sel1 otherSEL:(SEL)sel2 {    Method fromMethod = class_getInstanceMethod([self class], sel1);    Method toMethod = class_getInstanceMethod([self class], sel2);    method_exchangeImplementations(fromMethod, toMethod);}

3:获取所有的变量,属性,方法, 协议名

/// 变量    unsigned int count = 0;    Ivar *ivarList = class_copyIvarList([People class], &count);    for (int i = 0; i < count; i++) {        // 变量的名        NSString *ivarName = [NSString stringWithUTF8String:ivar_getName(ivarList[i])];        // 变量的类型        NSString *ivarType = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivarList[i])];    }
unsigned int count = 0;    /// 属性    objc_property_t *propertyList = class_copyPropertyList([People class], &count);    /// 方法    Method *methodList = class_copyMethodList([People class], &count);    /// 协议    class_copyProtocolList([People class], &count);

4 动态添加方法

+ (BOOL)resolveClassMethod:(SEL)sel {    }+ (BOOL)resolveInstanceMethod:(SEL)sel {    }

 重定向

- (id)forwardingTargetForSelector:(SEL)aSelector {    }

 转发

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {    }- (void)forwardInvocation:(NSInvocation *)anInvocation {    }

 

转载于:https://www.cnblogs.com/jisa/p/9467218.html

你可能感兴趣的文章
Public Key Retrieval is not allowed错误
查看>>
Unable to load authentication plugin 'caching_sha2_password'.错误
查看>>
The server time zone value '乱码' 错误
查看>>
require.js的用法
查看>>
基础语言知识C++
查看>>
如何使电脑彻底崩溃!!!!(不要干坏事哦)
查看>>
简单练习题
查看>>
《A First Course in Probability》-chaper7-极限定理-强大数定理
查看>>
Android基于TrafficStats实现流量实时监测
查看>>
第3周实践项目7 删除链表元素最大值
查看>>
Java常用类之Properties类
查看>>
[UVA 11825] Hackers' Crackdown
查看>>
Google今天叫——谷歌
查看>>
Android中关闭DatePicker和NumberPicker等Picker类的可编辑模式
查看>>
jquery中利用队列依次执行动画
查看>>
reverie_mjp
查看>>
阅读笔记六
查看>>
J2EE(五)——servlet初识
查看>>
requests 可以玩接口自动化测试,爬虫也是可以滴
查看>>
20160419__第1课_第6课
查看>>