1

E.g, we have

typedef id(^func)(id);
func read_file = ^(NSString *path_to_file) {
     return [NSString stringWithContentsOfFile:path_to_file encoding:NSUTF8StringEncoding error:NULL];
};

I wonder how can we get the name of this block if I passed it as a parameter in some function call? E.g,

fileOperator(read_file); // I want to print the block's name in this function.

Thank you.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
Kun Hu
  • 417
  • 5
  • 11

1 Answers1

6

I wonder how can we get the name of this block if I passed it as a parameter in some function call?

You can't get the name of a block passed as a parameter any more than you can get the name of an int variable passed as a parameter. The name isn't part of the block... the name is associated with a variable that contains the block.

Assuming you've passed the block as a parameter, you should use the parameter name to refer to the block just as you'd use the name of an int parameter to refer to the integer value passed into a function or method.

Caleb
  • 124,013
  • 19
  • 183
  • 272