Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Block syntax error from Apple example 'Creating a Block'

Using apple's example from the docs

float (^oneFrom)(float);


oneFrom = ^(float aFloat) {

  float result = aFloat - 1.0;

  return result;

};

I get two errors:

  1. Redefinition of 'oneFrom' with a different type: 'int' vs 'float(^)(float)'
  2. Type specifier missing, defaults to 'int'

Also from the doc..

If you don’t explicitly declare the return value of a block expression, it can be automatically inferred from the contents of the block. If the return type is inferred and the parameter list is void, then you can omit the (void) parameter list as well. If or when multiple return statements are present, they must exactly match (using casting if necessary).

like image 947
estobbart Avatar asked Jul 03 '13 13:07

estobbart


1 Answers

You can't define blocks on file scope, only in functions. This works as expected:

void foo (void)
{
    float (^oneFrom)(float);
    oneFrom = ^(float aFloat) {
        float result = aFloat - 1.0;
        return result;
    };
}
like image 96
Nikolai Ruhe Avatar answered Oct 01 '22 15:10

Nikolai Ruhe