在xcode6黄金大师,现在使用objc_msgSend抛出一个语法错误,说参数的数量是错误的
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);
论据是“无效的”或可变的? 我不明白我应该怎么称呼这个。
请参阅上面提到的几行。
/* * ... * * 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));
我已经检查出来了,主要问题是@Jerry Krinock在接受答案的评论中说:
- 转到您的项目生成设置
- searchobjc_msgSend
- 将其值设置为“否”而不是“是”
如果它发生在一个cocoapod库中,你至less需要更新到cocoapods 36.1。如果你不能这样做,在你的podfile的末尾添加下面的代码:
(将AFNetworkingreplace为您看到这些错误的窗格)
when /AFNetworking/ target.build_configurations.each do |config| config.build_settings['ENABLE_STRICT_OBJC_MSGSEND'] = "NO" end