ios - 在 xcode6 gold master 中,使用 objc_msgSend 现在会抛出一个语法错误,指出参数数量错误

标签 ios xcode6

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

“函数调用的参数过多,预​​期为 0,实际为 2”

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

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

参数是“void”还是可变参数?!我不明白我应该怎么调用它。

最佳答案

只看上面你提到的几行。

 /* 
  * ...
  *
  * 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));

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

关于ios - 在 xcode6 gold master 中,使用 objc_msgSend 现在会抛出一个语法错误,指出参数数量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25852079/

相关文章:

ios - IBM Worklight 6.1 - 如何将自定义编写的 native 代码从 Eclipse 添加到 Xcode?

ios - 可变细胞永远不会突变

iPhone - UIScrollView ...检测触摸坐标

ios - 根据 subview 的内容大小调整 ScrollView

ios - Xcode 6 中的文档方法和枚举

ios - UITableViewCell 第一次不显示数据

ios - 你能在 NSUserDefaults 中保存 Parse 信息吗? (IOS)

ios - NSException 对于 ios 开发人员很有用,它的子类有哪些类型

ios - 自动布局中固定和对齐之间的区别?

ios - 文件无法打开,因为您没有权限