xcode 函数调用的参数太多,预期为 0,有 3 个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24922913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Too many arguments to function call, expected 0, have 3
提问by Alex the Ukrainian
This compiles/works fine with Xcode 5, but causes a compile error with Xcode 6 Beta 4:
这在Xcode 5 中编译/工作正常,但在Xcode 6 Beta 4 中导致编译错误:
objc_msgSend(anItem.callback_object,
NSSelectorFromString(anItem.selector), dict);
This is a 3rd-party component, so while I have the source code, it's not really my code and I'm hesitant to change it much (despite my personal opinion of 'wtf why are they using objc_msgSend
??').
这是一个3rd-party 组件,所以虽然我有源代码,但它并不是我的代码,我很犹豫要不要改变它(尽管我个人认为“wtf 为什么他们使用objc_msgSend
??”)。
Image with possibly useful detail (error in error browser):
带有可能有用细节的图像(错误浏览器中的错误):
采纳答案by Alex the Ukrainian
I found the answer, and it's in Session 417 from 2014 WWDC "What's New in LLVM". If you find this code in a 3rd party library, such as Apsalar, updating to the latest version fixes it (probably because it's not distributed as a lib, ha). For an example of casting of these calls, see THObserversAndBinders library - I'm using it and noticed that the author updated the code, such as here:
我找到了答案,它在 2014 年 WWDC“LLVM 中的新功能”的第 417 次会议中。如果您在第 3 方库(例如 Apsalar)中找到此代码,则更新到最新版本可以修复它(可能是因为它不是作为 lib 分发的,哈哈)。有关这些调用的转换示例,请参阅 THObserversAndBinders 库 - 我正在使用它并注意到作者更新了代码,例如:
https://github.com/th-in-gs/THObserversAndBinders/blob/master/THObserversAndBinders/THObserver.m
https://github.com/th-in-gs/THObserversAndBinders/blob/master/THObserversAndBinders/THObserver.m
回答by james_alvarez
If you think having to do this is annoying and pointless you can disable the check in the build settings by setting 'Enable strict checking of objc_msgSend Calls' to no
如果您认为必须这样做很烦人且毫无意义,您可以通过将“启用对 objc_msgSend 调用的严格检查”设置为 no 来禁用构建设置中的检查
回答by Vladimir Grigorov
Just to spare watching a WWDC video, the answer is you need to strong type objc_msgSend for the compiler to build it:
只是为了节省观看 WWDC 视频,答案是您需要强类型 objc_msgSend 以便编译器构建它:
typedef void (*send_type)(void*, SEL, void*);
send_type func = (send_type)objc_msgSend;
func(anItem.callback_object, NSSelectorFromString(anItem.selector), dict);
Here is another sample when calling instance methods directly, like this:
这是直接调用实例方法时的另一个示例,如下所示:
IMP methodInstance = [SomeClass instanceMethodForSelector:someSelector];
methodInstance(self, someSelector, someArgument);
Use strong type for methodInstance to make LLVM compiler happy:
为 methodInstance 使用强类型使 LLVM 编译器满意:
typedef void (*send_type)(void*, SEL, void*);
send_type methodInstance = (send_type)[SomeClass instanceMethodForSelector:someSelector];
methodInstance(self, someSelector, someArgument);
Do not forget to set send_type's return and argument types according to your specific needs.
不要忘记根据您的特定需要设置 send_type 的返回和参数类型。
回答by Maciej Swic
This could also be caused by running pod install
using Cocoapods 0.36.beta.2
. I have reported the issue to CocoaPods. "Workaround" by using CocoaPods 0.35
这也可能是由于pod install
使用 Cocoapods运行造成的0.36.beta.2
。我已经向 CocoaPods 报告了这个问题。使用 CocoaPods 的“解决方法”0.35
回答by arunjos007
回答by Sahil Kapoor
Maciej Swic is right.This is caused in Pods after updating Cocoapods to 0.36.beta.2. I found a simple workaround by type casting objc_msgSend:
Maciej Swic 是对的。这是在将 Cocoapods 更新到 0.36.beta.2 后在 Pods 中引起的。我通过类型转换 objc_msgSend 找到了一个简单的解决方法:
id (*typed_msgSend)(id, SEL) = (void *)objc_msgSend;
id<MyProtocol> obJ = typed_msgSend(controller, @selector(myselector));
回答by Al Lelopath
I was getting this error with QuickDialog. Following on to james_alvarez's answer but for AppCode, go to Project Settings
, then click on QuickDialog
under Project/Shared Settings, scroll down to ENABLE_STRICT_OBJC_MSGSEND and enter NO for Debug and Release.
我在 QuickDialog 中遇到了这个错误。按照 james_alvarez 的回答,但对于AppCode,转到Project Settings
,然后QuickDialog
在 Project/Shared Settings 下单击,向下滚动到 ENABLE_STRICT_OBJC_MSGSEND 并为 Debug and Release 输入 NO。
回答by José Manuel Sánchez
You can also disable this with a post install hook:
您还可以使用安装后挂钩禁用此功能:
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['ENABLE_STRICT_OBJC_MSGSEND'] = 'NO'
end
end
end
回答by Валерий Кобзарь
#include <objc/message.h>
void foo(void *object) {
typedef void (*send_type)(void *, SEL, int);
send_type func = (send_type)objc_msgSend;
func(object, sel_getUid("foo:"), 5);
}
回答by Munim Dibosh
Following the accepted answer- to find the answer in the given codebase might be cumbersome for few, so here's the quick snap that should solve this problem.
按照公认的答案 - 在给定的代码库中找到答案可能对少数人来说很麻烦,所以这里是应该解决这个问题的快速快照。
I edited the code in ActionSheetPicker
in my project, which was causing me the same problem, like this -
我ActionSheetPicker
在我的项目中编辑了代码,这导致了我同样的问题,就像这样 -
- (void)notifyTarget:(id)target didSucceedWithAction:(SEL)action origin:(id)origin {
if ([target respondsToSelector:action]) {
((id (*)(id, SEL, NSDate *, id))objc_msgSend)(target, action, self.selectedDate, origin);
return;
} else if (nil != self.onActionSheetDone) {
self.onActionSheetDone(self, self.selectedDate, origin);
return;
}
NSAssert(NO, @"Invalid target/action ( %s / %s ) combination used for ActionSheetPicker", object_getClassName(target), (char *)action);
}
So look at the change that objc_msgSend
portion has, compared to your current code.
The idea is to include the type of the parameters you are passing to objc_msgSend
因此objc_msgSend
,与您当前的代码相比,请查看该部分的更改。这个想法是包含您传递给的参数的类型objc_msgSend