IMP self _cmd
The only way to circumvent dynamic binding is to get the address of a method and call it directly as if it were a function. This might be appropriate on the rare occasions when a particular method will be performed many times in succession and you want to avoid the overhead of messaging each time the method is performed.
With a method defined in the NSObject class, methodForSelector:, you can ask for a pointer to the procedure that implements a method, then use the pointer to call the procedure. The pointer that methodForSelector: returns must be carefully cast to the proper function type. Both return and argument types should be included in the cast.
The example below shows how the procedure that implements the setFilled: method might be called:
void (*setter)(id, SEL, BOOL); |
int i; |
setter = (void (*)(id, SEL, BOOL))[target |
methodForSelector:@selector(setFilled:)]; |
for ( i = 0 ; i < 1000 ; i++ ) |
setter(targetList[i], @selector(setFilled:), YES); |
The first two arguments passed to the procedure are the receiving object (self) and the method selector (_cmd). These arguments are hidden in method syntax but must be made explicit when the method is called as a function.
Using methodForSelector: to circumvent dynamic binding saves most of the time required by messaging. However, the savings will be significant only where a particular message is repeated many times, as in the for loop shown above.
Note that methodForSelector: is provided by the Cocoa runtime system; it’s not a feature of the Objective-C language itself.
IMP self _cmd的更多相关文章
- objc_msgSend消息传递学习笔记 – 消息转发
该文是 objc_msgSend消息传递学习笔记 – 对象方法消息传递流程 的基础上继续探究源码,请先阅读上文. 消息转发机制(message forwarding) Objective-C 在调用对 ...
- Objective-C中的消息发送总结
关于OC中的消息发送的实现,在去年也看过一次,当时有点不太理解,但是今年再看却很容易理解. 我想这跟知识体系的构建有关,如果你不认识有砖.水泥等这些建筑的基本组成部分,那么我们应该很难理解建筑是怎么建 ...
- 刨根问底KVO原理
介绍 KVO( NSKeyValueObserving )是一种监测对象属性值变化的观察者模式机制.其特点是无需事先修改被观察者代码,利用 runtime 实现运行中修改某一实例达到目的,保证了未侵入 ...
- OC方法交换swizzle详细介绍——不再有盲点
原文链接:https://www.cnblogs.com/mddblog/p/11105450.html 如果对方法交换已经比较熟悉,可以跳过整体介绍,直接看常见问题部分 整体介绍 方法交换是runt ...
- Objective-C 消息发送与转发机制原理(摘)
八面玲珑的 objc_msgSend 此函数是消息发送必经之路,但只要一提 objc_msgSend,都会说它的伪代码如下或类似的逻辑,反正就是获取 IMP 并调用: id objc_msgSend( ...
- Runtime 中的 _cmd、 IMP
IMP IMP-指向实际执行函数体的函数指针 #if !OBJC_OLD_DISPATCH_PROTOTYPES typedef void (*IMP)(void /* id, SEL, ... */ ...
- exp/imp 与 expdp/impdp 区别
在平常备库和数据库迁移的时候,当遇到大的数据库的时候在用exp的时候往往是需要好几个小时,耗费大量时间.oracle10g以后可以用expdp来导出数据库花费的时间要远小于exp花费的时间,而且文件也 ...
- EXP/IMP 导出生产库表的指定数据到测试库一例
一般来讲,EXP/IMP是上一代导出导入程序,EXPDP/IMPDP是新一代的导出导入程序.对于大数据量的导出导入首选EXPDP/IMPDP,可以用到并行度,对表空间等操作上也更加的灵活.对于小数据量 ...
- Oracle数据库验证IMP导入元数据是否会覆盖历史表数据
场景:imp导入数据时,最终触发器报错退出,并未导入存储过程.触发器.函数. 现在exp单独导出元数据,然后imp导入元数据,验证是否会影响已导入的表数据. 测试环境:CentOS 6.7 + Ora ...
随机推荐
- 【Netcore】使用 Magic生成器 ,零代码实现CRUD - HTTP REST 之接口
软件介绍: Magic是一个CRUD后端生成器,内置于ASP.NET内核中.它的目的是让你“神奇地”做一些无聊的事情,通过使用自动化技术,创建80%的CRUD端点,自动包装MySQL或MS SQL S ...
- Qt SDK Issue cstdlib: fatal error: stdlib.h: No such file or directory
*To reproduce the issue, I've tried the following solutions which did not help:* *1) Globally remove ...
- 创建新react项目 运行npm start 报错踩过的坑
1.看react官网创建新的react项目 :npx create-react-app my-app cd到my-app npm start 遇见如下报错 这是因为电脑本地git的原因 ,不是 ...
- 乘法器——Wallace树型乘法器
博主最近在看乘法器相关的知识,发现现在用的比较多的是booth编码的乘法器和Wallace树型乘法器,当然两者并不是互斥的关系,他们也可以结合使用.在这里给大家介绍一下Wallace树型乘法器,希望能 ...
- Excel数据都在一列,如何批量转置
Evernote Export Excel数据都在一列,如何批量转置 创建时间: 2019-10-21 星期一 13:41 作者: 苏苏 标签: excel, 转置 问题 Excel数据都 ...
- 微信小程序:防止多次点击跳转(函数节流)
场景 在使用小程序的时候会出现这样一种情况:当网络条件差或卡顿的情况下,使用者会认为点击无效而进行多次点击,最后出现多次跳转页面的情况,就像下图(快速点击了两次): 解决办法 然后从 轻松理解JS函数 ...
- SPA单页面应用和MPA多页面应用(转)
原文:https://www.jianshu.com/p/a02eb15d2d70 单页面应用 第一次进入页面时会请求一个html文件,刷新清除一下,切换到其他组件,此时路径也相应变化,但是并没有新的 ...
- In-Memory:哈希索引
SQL Server 2016支持哈希查找,用户可以在内存优化表(Memory-Optimized Table)上创建哈希索引(Hash Index),使用Hash 查找算法,实现数据的极速查找.在使 ...
- mybatis-plus 主键自增问题
主键不自增:返回值是插入的条数 <insert id="add" parameterType="EStudent"> insert into TSt ...
- 一张图看懂SharpBarcode
通过下面的图片,可以瞬间看懂整个SharpBarcode类库的脉络.