Why can't I create a variable in a switch block?

Weird:

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

        break;
}

So it is not possible to create a local variable in the switch block block?

+3
source share
1 answer

Have you tried to add braces?

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

            break; 
        }
}
+12
source

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


All Articles