【问题标题】:getting return value type of an instance method in runtime在运行时获取实例方法的返回值类型
【发布时间】:2012-05-24 13:50:50
【问题描述】:

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

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

或:

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

我看过post where someone explain the way to have that with objective-c runtime

Method m = class_getClassMethod([_instance class], _selector);

char ret[256];
method_getReturnType(m, ret, 256);
NSLog(@"Return type: %s", ret);

但输出并不像 ret 是空的。

真的,知道它是 void 还是有返回类型就足够了,但我不知道在哪里搜索。我已经阅读了objective-c runtime reference,但我发现的唯一内容是method_getReturnType...。有什么想法吗?

【问题讨论】:

  • 你真的不想将这种内省用于教育实验。沿着这条路走下去是疯狂和许多维护噩梦。
  • 感谢广告!如果您对如何操作有更好的建议,我愿意接受;)
  • 你想做什么? IE。为什么你需要知道这个?可能值得提出一个新问题。

标签: objective-c ios objective-c-runtime


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    搜索了一会,我找到了使用 Spotify 做这类东西的库,名字叫 MAObjcRuntime,你可以找到它here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 2023-02-16
      • 1970-01-01
      • 2015-05-23
      相关资源
      最近更新 更多