James Webster
James Webster

Reputation: 32066

Block Syntax Objective C

I've just read this snippet from another answer:

When you create a block with the ^{} syntax...

I understand this syntax, and use it regularly, however I inferred from this that there might be other syntaxes that can be used for creating blocks. Are there? If there are, are there any advantages of the different syntaxes?

Upvotes: 7

Views: 2136

Answers (2)

Lorenzo B
Lorenzo B

Reputation: 33428

If your question is about block literal syntax (the one used for anonymous functions), here is the general form

^ return type (arguments list) {expressions}

Based on the that, you can omit the return type

^ (arguments list) {expressions}

since it can be inferred from the return type. If there is no return value, void is the choice.

Furthermore, you can write

^ {expressions}

if there are no arguments.

This is the same as

^ void (void) { NSLog(@"Something"); }

Upvotes: 12

giorashc
giorashc

Reputation: 13713

By looking here I think the only differences are if you use return types/arguments

Upvotes: 5

Related Questions