Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C: Is there an -invoke on blocks that takes parameters?

As you may be aware, blocks take -invoke:

void(^foo)() = ^{
    NSLog(@"Do stuff");
};

[foo invoke];  // Logs 'Do stuff'

I would like to do the following:

void(^bar)(int) = ^(int k) {
     NSLog(@"%d", k);
};

[bar invokeWithParameters:7];   // Want it to log '7', but no such instance method

The ordinary argument-less -invoke works on bar, but it prints a nonsense value.

I can't find a direct message of this kind I can send to a block, nor can I find the original documentation that would describe how blocks take -invoke. Is there a list of messages accepted by blocks?

(Yes, I have tried to use class_copyMethodList to extract a list of methods from the runtime; there appear to be none.)

Edit: Yes, I'm also aware of invoking the block the usual way (bar(7);). What I'm really after is a selector for a method I can feed into library code that doesn't take blocks (per-se).

like image 231
DrJosh9000 Avatar asked Sep 20 '11 00:09

DrJosh9000


1 Answers

You can invoke it like a function:

bar(7);

There's even an example in the documentation that uses exactly the same signature. See Declaring and Using a Block.

The best reference on the behavior of blocks is the Block Language Specification(RTF) document. This mentions certain methods that are supported (copy, retain, etc.) but nothing about an -invoke method.

like image 50
Caleb Avatar answered Nov 16 '22 03:11

Caleb