Block syntax error from Apple's Create Block example

Using the apple example from the documentation

float (^oneFrom)(float); oneFrom = ^(float aFloat) { float result = aFloat - 1.0; return result; }; 

I get two errors:

  • Overriding "oneFrom" with another type: 'int' vs 'float (^) (float)'
  • Missing type specifier, default is 'int'

Also from the document ..

If you do not explicitly declare the return value of the expression block, it can be automatically deduced from the contents of the block. If the return type is displayed and the parameter list is invalid, then you can also omit the parameter list (void). If or when multiple return statements are present, they must match exactly (using casting if necessary).

+6
source share
2 answers

You cannot define blocks in a file area, only in functions. This works as expected:

 void foo (void) { float (^oneFrom)(float); oneFrom = ^(float aFloat) { float result = aFloat - 1.0; return result; }; } 
+3
source

this block has no return type, and the default return type is invalid, you will need

 float (^oneFrom)(float); oneFrom = ^ float (float aFloat) { float result = aFloat - 1.0; return result; }; 

here is the best example of blocks

-1
source

Source: https://habr.com/ru/post/948630/


All Articles