欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

macOS objc_msgSend

程序员文章站 2022-03-12 07:57:21
变化macOS升级到10.15后,宏OBJC_OLD_DISPATCH_PROTOTYPES的值变为0,导致objc_msgSend 定义发生变化。#if !OBJC_OLD_DISPATCH_PROTOTYPESOBJC_EXPORT void objc_msgSend(void /* id self, SEL op, ... */ ) OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0);OBJC_EXPORT void objc_msgSendSuper(vo....
  1. 变化
  • macOS升级到10.15后,宏OBJC_OLD_DISPATCH_PROTOTYPES的值变为0,导致objc_msgSend 定义发生变化。
#if !OBJC_OLD_DISPATCH_PROTOTYPES
OBJC_EXPORT void objc_msgSend(void /* id self, SEL op, ... */ )
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0);
OBJC_EXPORT void objc_msgSendSuper(void /* struct objc_super *super, SEL op, ... */ )
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0);
#else
/** 
 * Sends a message with a simple return value to an instance of a class.
 * 
 * @param self A pointer to the instance of the class that is to receive the message.
 * @param op The selector of the method that handles the message.
 * @param ... 
 *   A variable argument list containing the arguments to the method.
 * 
 * @return The return value of the method.
 * 
 * @note When it encounters a method call, the compiler generates a call to one of the
 *  functions \c objc_msgSend, \c objc_msgSend_stret, \c objc_msgSendSuper, or \c objc_msgSendSuper_stret.
 *  Messages sent to an object’s superclass (using the \c super keyword) are sent using \c objc_msgSendSuper; 
 *  other messages are sent using \c objc_msgSend. Methods that have data structures as return values
 *  are sent using \c objc_msgSendSuper_stret and \c objc_msgSend_stret.
 */
OBJC_EXPORT id objc_msgSend(id self, SEL op, ...)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0);
/** 
 * Sends a message with a simple return value to the superclass of an instance of a class.
 * 
 * @param super A pointer to an \c objc_super data structure. Pass values identifying the
 *  context the message was sent to, including the instance of the class that is to receive the
 *  message and the superclass at which to start searching for the method implementation.
 * @param op A pointer of type SEL. Pass the selector of the method that will handle the message.
 * @param ...
 *   A variable argument list containing the arguments to the method.
 * 
 * @return The return value of the method identified by \e op.
 * 
 * @see objc_msgSend
 */
OBJC_EXPORT id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0);
#endif 
  1. 方案
  • 原代码:
Class cls = objc_getClass("NSApplication");
objc_object* appInst = objc_msgSend((objc_object*)cls, 
                       sel_registerName("sharedApplication"));
  • 修改后:
Class cls = objc_getClass("NSApplication");
objc_object* appInst = ((objc_object * (*)(objc_object *, SEL)) objc_msgSend)((objc_object*)cls, 
                       sel_registerName("sharedApplication"));

本文地址:https://blog.csdn.net/luoshabugui/article/details/108579532