12

I'm trying to define a block that takes a block as an argument.

What's wrong with the following line of code?

id (^cacheResult)(NSString *, id(^)(void)) = ^(NSString *name, id(^)(void)block) { NSObject *item = nil; block(); return item; }; 

Why does the compiler keep giving errors like Parameter name omitted and Expected ")"?

3 Answers 3

10
id (^cacheResult)(NSString *, id(^)(void)) = ^(NSString *name, id(^block)(void)) { NSObject *item = nil; block(); return item; }; 

Blocks have similar syntax to function pointers. You have to declare block name after the ^

Sign up to request clarification or add additional context in comments.

Comments

10

This is why typedef was invented. Embedding function pointers or block types like this is a pain. Try this instead:

typedef id (^ InnerBlock)(void); typedef id (^ OuterBlock)(NSString *, InnerBlock); 

It'll make working with block types a lot easier to read. :)

1 Comment

ah, thanks for the pointer(no pun intended). I find blocks definition really hard to read sometimes and typedef is indeed the perfect solution! Though my particular error was misunderstanding where to place the block variable name.
3

Did you possibly mean id(^block)(void) on the RHS of the assignment?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.