3

Strange:

switch(type) {
    case NSFetchedResultsChangeInsert:
        int x = 5; // error: "Expected expression before int"

        break;
}

So it isn't possible to create a local variable in an switch-case-block?

dontWatchMyProfile
  • 45,440
  • 50
  • 177
  • 260
  • possible duplicate of [Can I declare variables inside an Objective-C switch statement?](http://stackoverflow.com/questions/1115304/can-i-declare-variables-inside-an-objective-c-switch-statement) – Justin Ethier Jun 14 '10 at 20:24
  • Does it work if you make the case an actual block? like in `case foo: { int x = 5; ... }`? Remember that `switch/case` in C is just a razor-thin wrapper around a few jump targets and nothing else. That's why Duff's device is so ingenious as well. – Joey Jun 14 '10 at 20:25

1 Answers1

12

Did you try adding curly braces?

switch(type) {
    case NSFetchedResultsChangeInsert:
        {
            int x = 5; // error: "Expected expression before int"

            break; 
        }
}
dcp
  • 54,410
  • 22
  • 144
  • 164
  • You need to declare a new scope for some reason. My C isn't good enough to know. – Tom H Jun 14 '10 at 20:29
  • It is an oddity from the C standard. There is some weird syntactical edge case [that I'm perpetually running into] that prevents the compiler from "just working". – bbum Jun 14 '10 at 22:01