美文网首页ios面试题iOS开发
OS objc_msgSend 报错解决方案 以及 内存泄漏的问

OS objc_msgSend 报错解决方案 以及 内存泄漏的问

作者: 充满活力的早晨 | 来源:发表于2018-04-10 09:56 被阅读137次

    objc_msgSend(self, @selector(doSomething), self);====>这个函数使用会报错:Too many arguments to function call, expected 0, have 3
    错解决方案:选中项目 - Project - Build Settings - ENABLE_STRICT_OBJC_MSGSEND 将其设置为 NO 即可


    xcode配置

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

    image

    苹果官方文档:

    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

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

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

    相关文章

      网友评论

        本文标题:OS objc_msgSend 报错解决方案 以及 内存泄漏的问

        本文链接:https://www.haomeiwen.com/subject/cmrghftx.html