c - 获取协议(protocol)中方法 block 的 NSMethodSignature

标签 c objective-c objective-c-blocks swift-protocols

我正在尝试在协议(protocol)方法中获取 block 的签名。

这是一个示例协议(protocol):

@protocol ProtocolSample <NSObject>
- (void) doSomething: (void (^) (NSString *))a_block;
@end

我可以使用以下命令获取 doSomething 的签名:

Protocol *protocol_sample = @protocol(ProtocolSample);

unsigned int outCount;
struct objc_method_description *method_description_list = protocol_copyMethodDescriptionList(protocol_sample, YES, YES, &outCount);

struct objc_method_description method_description = method_description_list[0];

NSMethodSignature *signature = [NSMethodSignature signatureWithObjCTypes:method_description.types];

我得到的签名是:v@:@?

我的目标是获得a_block的签名。我尝试了很多方法,包括以下:

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];

void *block;
[invocation getArgument:&block atIndex:2];

但是block始终为NULL

如何获得a_block的签名?

最佳答案

终于找到答案了:const char *_protocol_getMethodTypeEncoding(Protocol *, SEL, BOOL isRequiredMethod, BOOL isInstanceMethod);

该方法将为您提供任何选择器的完整签名!

关于c - 获取协议(protocol)中方法 block 的 NSMethodSignature,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54775394/

相关文章:

c - 以下c语言代码的正确输出是什么?

c - 传递专门定义为字符串的地址的段错误

c - armcc链接错误错误: L6769E in armcc while building a C project,是什么问题?

objective-c - Objective C block 和变量

objective-c - 管理调用委托(delegate)回调 block 的对象的内存

iphone - NSComparisonResult 和 NSComparator - 它们是什么?

c - scanf 和 printf 仅使用整数指针在数组中显示 10 个数字//不能使用整数而不是 A[]。 +1 更多

objective-c - NSDateFormatter:如何将带有 'GMT' 的日期字符串转换为本地 NSDate?

ios - QuickBlox - 从后台调用用户/创建 session

ios - 如何显示一个 ViewController 然后关闭它以显示一个 UISplitViewController?