在运行时获取实例方法的返回值类型



我想在运行时获取实例的返回值类。问题是我有一个SEL类型的var,我在其中存储了一个选择器。我有一个名为 id _instance 的变量,它指向我知道它执行选择器的实例。在执行该方法之前,我想知道我是否必须这样做:

NSObject* returnValue=[_instance performSelector:_selector withObject:params.params];

或:

[_instance performSelector:_selector withObject:params.params];

我读过一篇文章,有人解释了使用objective-c运行时的方法:

Method m = class_getClassMethod([_instance class], _selector);
char ret[256];
method_getReturnType(m, ret, 256);
NSLog(@"Return type: %s", ret);

但是输出不像 ret 是空的。

真的知道它是空白还是返回类型就足够了,但我不知道在哪里搜索。我已经阅读了 objective-c 运行时参考,但我唯一发现的是method_getReturnType...。知道吗?

如果要查找实例方法,则需要使用 class_getInstanceMethod 而不是 class_getClassMethod 。类方法和实例方法显然是不同的东西。

搜索了一段时间后,我找到了使用Spotify进行此类操作的库,名称为MAObjcRuntime,您可以在此处找到它

最新更新