0

Why am I not able to use this?

__block NSString *tableStrings[4][2];

[userValues enumerateObjectsUsingBlock:^(NSNumber *userScore, NSUInteger idx, BOOL *stop) {
        tableStrings[idx][0] = @"< 5";
        tableStrings[idx][1] = @"> 95";
}];

The compiler is yelling at me of "Cannot refer to declaration with an array type inside block". I was under the impression that denoting __block before the variable would allow this to be done. I can make it work with using NSString[x][x] but I'm curious as to why this is not allowed.

random
  • 8,568
  • 12
  • 50
  • 85

1 Answers1

0

Blocks cannot access array variables of automatic or __block storage from the enclosing scope. That's just a restriction of blocks. It's because both of those things requires being able to copy the variable. And array type is not assignable.

It would be possible for them to make a special case for arrays, that arrays are copied element-by-element. C++11 lambdas makes this special case, so they can capture arrays by value, even though arrays are not assignable. However, the blocks people didn't bother to make this special case.

newacct
  • 119,665
  • 29
  • 163
  • 224