iOS 64-bit architecture 使用objc_msgSend要小心

在iOS64-bit下,使用objc_msgSend直接调用方法会造成崩溃,报错xc_bad_access,具体原因看苹果文档https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaTouch64BitGuide/ConvertingYourAppto64-Bit/ConvertingYourAppto64-Bit.html

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.

Listing 2-14 Using a cast to call the Objective-C message sending
functions
- (int) doSomething:(int) x { … }
- (void) doSomethingElse {
int (action)(id, SEL, int) = (int ()(id, SEL, int)) objc_msgSend;
action(self, @selector(doSomething:), 0); }

大致就是,OBJC_EXPORT id objc_msgSend(id self, SEL op, …)最后的参数需要声明类型,所以使用应该像这样

void (*objcMsgSend)(id,SEL,id) = (void *)objc_msgSend;
objcMsgSend(plugin, selector, cmd);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值