Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 626
James Webster Avatar asked Jul 01 '12 11:07

James Webster


People also ask

What is block syntax in Objective-C?

Blocks are a language-level feature added to C, Objective-C and C++, which allow you to create distinct segments of code that can be passed around to methods or functions as if they were values. Blocks are Objective-C objects, which means they can be added to collections like NSArray or NSDictionary .

What does the symbol mean in Objective-C?

That symbol is used to declare block. For more information read here Blocks Programming Topics. Some more info: Block objects are a C-level syntactic and runtime feature.

What is Typedef Objective-C?

The typedef function is used to assign the new name to the datatype. So that you can use a custom name for predefined long names of the datatypes.


2 Answers

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"); }
like image 67
Lorenzo B Avatar answered Oct 30 '22 20:10

Lorenzo B


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

like image 39
giorashc Avatar answered Oct 30 '22 22:10

giorashc