我是靠谱客的博主 谦让橘子,最近开发中收集的这篇文章主要介绍macOS objc_msgSend,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

  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"));

最后

以上就是谦让橘子为你收集整理的macOS objc_msgSend的全部内容,希望文章能够帮你解决macOS objc_msgSend所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部