在xcode6 gold master中,使用objc_msgSend现在抛出一个语法错误,说明参数的数量是错误的 [英] in xcode6 gold master, using objc_msgSend now throws a syntax error saying the number of arguments is wrong

查看:159
本文介绍了在xcode6 gold master中,使用objc_msgSend现在抛出一个语法错误,说明参数的数量是错误的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 id<MyProtocol> topLayoutGuideObj = objc_msgSend(viewController, @selector(myselector));

函数调用的参数太多,预期为0,有2

"Too many arguments to function call, expected 0, have 2"

但是,objc_msgSend的函数签名如下所示:

However, the function signature for objc_msgSend looks like this:

#if !OBJC_OLD_DISPATCH_PROTOTYPES
OBJC_EXPORT void objc_msgSend(void /* id self, SEL op, ... */ )
    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
OBJC_EXPORT void objc_msgSendSuper(void /* struct objc_super *super, SEL op, ... */ )
    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_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, ...)
    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

参数是无效还是可变参数?!我不明白我该怎么称呼它。

The arguments are "void" or variadic ?! I don't understand how I'm supposed to call this.

推荐答案

只看你上面几行。

 /* 
  * ...
  *
  * These functions must be cast to an appropriate function pointer type 
  * before being called. 
  */

您可以这样称呼:

#import <objc/runtime.h>
#import <objc/message.h>

id<MyProtocol> topLayoutGuideObj = ((id (*)(id, SEL))objc_msgSend)(viewController, @selector(myselector));

OR

id (*typed_msgSend)(id, SEL) = (void *)objc_msgSend;
id<MyProtocol> topLayoutGuideObj = typed_msgSend(viewController, @selector(myselector));

这篇关于在xcode6 gold master中,使用objc_msgSend现在抛出一个语法错误,说明参数的数量是错误的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆