Kun Hu
Kun Hu

Reputation: 427

How to get the name of block?

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.

Upvotes: 1

Views: 85

Answers (1)

Caleb
Caleb

Reputation: 124997

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.

Upvotes: 6

Related Questions