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?
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 .
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.
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.
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"); }
By looking here I think the only differences are if you use return types/arguments
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With