You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在静态方法文件中调用静态方法报错:no known instance method for selector 'test'

问题解决:no known instance method for selector 'test'

你这里的错误根源是混淆了C静态函数和Objective-C方法的调用规则:

  • 你定义的test()是C风格的静态void函数,并非Objective-C的实例/类方法
  • [self test]是Objective-C的消息发送语法,编译器会将其识别为调用名为test的实例方法,但该方法并不存在,且你的rddhandler函数里也未定义self变量(参数是self2)

解决方法

直接使用C函数的调用方式调用test,修改后代码如下:

static void rddhandler(id self2, SEL _cmd){
    test(); // 直接调用C静态函数
}

static void test() {
    NSLog(@"test");
}

如果你的需求是把test写成Objective-C静态方法,需调整定义和调用逻辑:

static void rddhandler(id self2, SEL _cmd){
    // 类方法调用:使用类名或[self2 class]
    [[self2 class] test];
}

// 定义Objective-C类方法
+ (void)test {
    NSLog(@"test");
}

内容的提问来源于stack exchange,提问作者user5405648

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 19:25:00