Compile Error with: switch, “expected expression before”

早过忘川 提交于 2019-11-28 04:02:42
Dan Olson

In normal C you'd have to enclose this in brackets in both cases. I suspect this may fix your problem:

case 1:
{
    NSLog(@"");
    NSString *aStr;
    break;
}

See this SO question for more info.

Another way to get around this problem is to put a statement between the case label and the first declaration as you've done in your working example above. See the comments and Quinn Taylor's answer for more info.

Quinn Taylor

You can't declare a variable as the first statement in a case without brackets, and in many other contexts in C-based languages. See Declaring variables inside a switch statement for details.

case 0: {
    Loading my nib file;
    break; 
}
case 1: {
    Loading another nib file;
    break; 
}
Note that if you don't have an assignment (x = y) right after the case it won't be a problem. For example:
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!