在xcode6 gold master中,使用objc_msgSend现在抛出语法错误,说明参数的数量是错误的

non*_*ont 17 ios xcode6

 id<MyProtocol> topLayoutGuideObj = objc_msgSend(viewController, @selector(myselector));
Run Code Online (Sandbox Code Playgroud)

"函数调用的参数太多,预期为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);
Run Code Online (Sandbox Code Playgroud)

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

rin*_*aro 28

只看你上面提到的几行.

 /* 
  * ...
  *
  * These functions must be cast to an appropriate function pointer type 
  * before being called. 
  */
Run Code Online (Sandbox Code Playgroud)

你可以这样称呼它:

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

id<MyProtocol> topLayoutGuideObj = ((id (*)(id, SEL))objc_msgSend)(viewController, @selector(myselector));
Run Code Online (Sandbox Code Playgroud)

要么

id (*typed_msgSend)(id, SEL) = (void *)objc_msgSend;
id<MyProtocol> topLayoutGuideObj = typed_msgSend(viewController, @selector(myselector));
Run Code Online (Sandbox Code Playgroud)

  • 显然,它出现在Xcode 6中的原因是因为默认情况下启用了"启用严格检查objc_msgSend调用"警告,并且在打开旧项目和"将项目更新为推荐设置"时默认启用. (11认同)

Ala*_*din 10

我已经检查过,主要的问题是@Jerry Krinock在接受回答的评论中说的;

  1. 转到您的项目构建设置
  2. 搜索objc_msgSend
  3. 将其值设置为"否"而不是"是"