http://blog.csdn.net/chenyong05314/article/details/42121001

2014-12-24 10:49 878人阅读 评论(0) 收藏 举报

转载自:  http://iloss.me/post/kai-fa/2014-12-09-objc_msgsend

之前一直用objc_msgSend,但是没注意apple的文档提示,所以突然objc_msgSend crash了。

之前32位的时候没问题,然后转换为64位之后就会发生EXC_BAD_ACCESS问题。

当然apple再文档(64-Bit Transition Guide for Cocoa Touch中有)中也有提到:

Dispatch Objective-C Messages Using the Method Function’s Prototype
An exception to the casting rule described above is when you are calling the objc_msgSend function or any other similar functions in the Objective-C runtime that send messages. Although the prototype for the message functions has a variadic form, the method function that is called by the Objective-C runtime does not share the same prototype. The Objective-C runtime directly dispatches to the function that implements the method, so the calling conventions are mismatched, as described previously. Therefore you must cast the objc_msgSend function to a prototype that matches the method function being called. Listing 2-14 shows the proper form for dispatching a message to an object using the low-level message functions. In this example, the doSomething: method takes a single parameter and does not have a variadic form. It casts the objc_msgSend function using the prototype of the method function. Note that a method function always takes an id variable and a selector as its first two parameters. After the objc_msgSend function is cast to a function pointer, the call is dispatched through that same function pointer

你必须先定义原型才可以使用,这样才不会发生崩溃

id Fun(int x,id y,...);
id (*action)(int,id) = (id (*)(int,id)) Fun;
action(1,@"s");

如果没有返回值id改为void

原来我们这么写:

objc_msgSend(self,@selector(doSomething:), 0);

按照apple的规范:

- (int) doSomething:(int) x { ... }
- (void) doSomethingElse {
int (*action)(id, SEL, int) = (int (*)(id, SEL, int)) objc_msgSend;
action(self, @selector(doSomething:), 0);
}

最终简化之后64位调用:

((void(*)(id, SEL,int))objc_msgSend)(self, @selector(doSomething:), 0);

最新文章

  1. java中对象多态时成员变量,普通成员函数及静态成员函数的调用情况
  2. mysql查询当前正在使用数据库
  3. Android手机_软件安装目录
  4. java输出流实现文件下载
  5. lk启动流程详细分析
  6. ubuntu上如何安装和卸载google chrome 浏览器
  7. 经典SQL语句大全之数据开发
  8. 改变和恢复view的方向
  9. linux下面的解压缩文件的命令
  10. api-gateway实践(09)支持rest服务注册
  11. Deployment descriptor
  12. 每日英语:The Right Way to Network
  13. vue使用render渲染&jsx
  14. HTTP 权威指南 第二章 URL 与资源
  15. Mac OS 10.12 - Gogland和在Windows中使用的不同!!
  16. [日常] crontab的秒执行和串行化和多进程实现
  17. -174dBm的含义
  18. Problem Z: 百鸡问题
  19. WordCounter Python实现
  20. Markdown博文快速转为微信文章

热门文章

  1. 今日Linux
  2. 第二十五篇 hashlib模块(* *)
  3. 问题 A: 完数
  4. linux基本操作1
  5. Spring温故而知新 – bean的装配
  6. ASP.NET MVC如何使用输出缓存
  7. HDFS集群和YARN集群
  8. IOI1998 Polygon [区间dp]
  9. POJ 2398 Toy Storage 二分+叉积
  10. nodejs npm insttall 带不带-g这个参数的区别