Reputation: 7031
BUT! add a random statement before:
Any thoughts?
Upvotes: 1
Views: 1089
Reputation: 4409
Just checked Kernighan&Ritchie for this. In the C specification is no line describing that a declaration in the the first statement of a 'case' is not allowed.
However most people consider it good programming to not declare values in a 'case'.
I think that the b) issue from EXC_BAD_ACCESS in statement block in Switch impact on imageViewPoistion is related to the same bug.
Upvotes: 0
Reputation: 78343
This isn't a switch-statement bug, it's a limitation in the C language. In C, the first statement after a case label cannot be a variable declaration. You can get around this by either declaring the variables before the switch statement or creating the variable inside a block of code (see below). Obviously, you can also re-order your code (if possible) so that another statement comes before the variable declaration.
Example 1:
CGRect newRect = CGRectZero;
switch( var ) {
case 0:
// do some stuff
break;
case 1:
default:
newRect = [someVar someMethodThatReturnsARect];
// other code
break;
}
Example 2:
switch( var ) {
case 0:
// do some stuff
break;
case 1:
default: {
CGRect newRect = [someVar someMethodThatReturnsARect];
// other code
break;
}
}
Example 3:
switch( var ) {
case 0:
// do some stuff
break;
case 1:
default:
// some code re-ordered to here
CGRect newRect = [someVar someMethodThatReturnsARect];
// rest of the other code
break;
}
Upvotes: 2