编译错误:开关,“期望的表达式之前” [英] Compile Error with: switch, "expected expression before"
问题描述
切换到追逐我已经重新创建我的问题,因为它是相当自我解释。
Cut to the chase I have recreated my problem as it is fairly self explanatory.
此操作正确无误:
switch (n) {
case 1:
NSLog(@"");
NSString *aStr;
break;
default:
break;
}
此编译错误 NSLog():
switch (n) {
case 1:
NSString *aStr;
break;
default:
break;
}
它会在编译时抛出一个错误
it throws an error at compile "Expected expression before 'NSString'"
我在这里缺少什么?
推荐答案
在正常的C中,在两种情况下都必须用括号括起来。我怀疑这可能解决您的问题:
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.
另一种方法是使用下面的命令:解决这个问题是在case标签和第一个声明之间放置一个语句,就像你在上面的工作示例中做的那样。有关详情,请参阅评论和Quinn Taylor的回答。
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.
这篇关于编译错误:开关,“期望的表达式之前”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!