Consider the following switch statement:
switch (buttonIndex) { case 0: [self fooWithCompletion:^{ [weakSelf finishEditing]; }]; break; case 1: // Error here [self barWithCompletion:^{ [weakSelf finishEditing]; }]; break; default: break; }
It causes the compiler error
Cannot jump from switch statement to this case label
on the line case 1:
.
Why is this happening and how do I fix it?
The switch statement doesn't accept arguments of type long, float, double,boolean or any object besides String.
Do not declare variables inside a switch statement before the first case label. According to the C Standard, 6.8.
If the cases were allowed to contain variables, the compiler would have to emit code that first evaluates these expressions and then compares the switch value against more than one other value. If that was the case, a switch statement would be just a syntactically-sugarized version of a chain of if and else if .
Variables are not allowed. The default statement is optional and can appear anywhere inside the switch block. In case, if it is not at the end, then a break statement must be kept after the default statement to omit the execution of the next case statement.
The block definition creates a new scope which seems to interfere with the compiler's ability to correctly interpret the switch statement.
Adding scope delimiters for each case label resolves the error. I think this is because the block's scope is now unambiguously a child of the case scope.
switch (buttonIndex) { case 0: { [self updateUserDataWithCompletion:^{ [weakSelf finishEditing]; }]; break; } case 1: { [self updateOtherDataWithCompletion:^{ [weakSelf finishEditing]; }]; break; } default: break; }
There's a bug open with LLVM for a similar issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With