【问题标题】:Too few arguments to function call, expected at least 2, have 0函数调用的参数太少,预计至少为 2,有 0
【发布时间】:2019-07-27 15:33:21
【问题描述】:

我写了以下代码:

@interface TestClass : NSObject
//
-(void)testLog;
//
+(void)testLog;
@end
//===============================
SEL sel = @selector(testLog);
IMP imp = class_getMethodImplementation([TestClass class], sel);
imp();
//===============================
SEL testSel = @selector(testLog);
IMP testImp = class_getMethodImplementation(objc_getMetaClass(class_getName([TestClass class])), testSel);
testImp();

我将Enable Strict Checking of objc_msgSend Calls设置为NO,但还是有这个错误。

为什么会出错?

【问题讨论】:

  • 已解决,修改objc_msgSend为YES,构建成功!

标签: objective-c runtime


【解决方案1】:

所有方法都至少需要两个参数:对正在调用该方法的对象(或类方法中的类)的引用,这是方法体内self 的值;和方法的选择器。这是您的通话中缺少的内容。

您可以在documentation 中给出的IMP 的定义中看到这一点:

id (*IMP)(id, SEL, ...)

附上解释:

此数据类型是指向实现该方法的函数开头的指针。此函数使用为当前 CPU 架构实现的标准 C 调用约定。第一个参数是一个指向 self 的指针(即这个类的特定实例的内存,或者,对于一个类方法,一个指向元类的指针)。第二个参数是方法选择器。方法参数如下。

HTH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 2016-09-05
    • 2017-04-01
    • 1970-01-01
    • 2022-01-07
    • 2019-01-23
    • 1970-01-01
    相关资源
    最近更新 更多