user11086841
user11086841

Reputation: 11

Get NSMethodSignature of a method block in a Protocol

I'm trying to get the signature of a block in a Protocol method.

Here's a sample protocol:

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

I am able to get the signature of doSomething using the following:

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];

The signature I get is: v@:@?

My goal is to get the signature of the a_block. I've tried many methods including the following:

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];

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

But block is always NULL.

How is it possible to get the signature of a_block?

Upvotes: 0

Views: 311

Answers (2)

L&#233;o Natan
L&#233;o Natan

Reputation: 57050

There is also

extern const char* _Block_signature(id block);

which you can use for any arbitrary block object.

Upvotes: 0

user11086841
user11086841

Reputation: 11

Finally found the answer: const char *_protocol_getMethodTypeEncoding(Protocol *, SEL, BOOL isRequiredMethod, BOOL isInstanceMethod);

The method will get you the full signature of any selector!

Upvotes: 1

Related Questions