3

I'm reading Apple's article about Objective-C runtime type encoding strings but I do not understand how to encode a method with a Block parameter.

For example, I have this :

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler 

I don't understand what to use for (void(^)())completionHandler

jscs
  • 63,694
  • 13
  • 151
  • 195
zeus
  • 12,173
  • 9
  • 63
  • 184

2 Answers2

1

All object types are @. Therefore I would expect this, because blocks are objects in Objective-C.

However, you can ask the runtime itself with method_getTypeEncoding().

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
1

When in doubt, use the @encode directive:

typedef void(^CompletionHandler)(void);

char *encoded = @encode(CompletionHandler);
NSLog(@"Encoded: %s", encoded);

prints @?.

To quote your documentation link:

@ An object (whether statically typed or typed id)
? An unknown type (among other things, this code is used for function pointers)

Sulthan
  • 128,090
  • 22
  • 218
  • 270