Posts Tagged ‘ case

“Expected Expression” when declaring and initializing NSInteger in a switch statement

I ran across a strange problem today. Tried something like this:

switch(controlVariable) {

case 0:

NSInteger x = kSomeConstant;

// some other code…

break;

case 1:

// …

default:

// …

}

And to my surprise, XCode complained saying that “Parse error: Expected Expression”. Upon googling, this is what I found.

So, the solution is to put some statement before you initialize NSInteger. If there is no statement that logically fit, you can simply insert a semi-colon “;” before declaring and initializing the integer.

switch(controlVariable) {

case 0:

;

NSInteger x = kSomeConstant;

// some other code…

break;

case 1:

// …

default:

// …

}