在 switch 语句中声明变量
我看到了这个问题的一些答案,我明白了——你不能在 switch
内声明和分配变量。 但我想知道以下抛出错误是否正确
错误:“int”之前有预期的表达式
代码:
switch (i) {
case 0:
int j = 1;
break;
}
为什么在它之前调用 NSLog()
不会导致错误?
switch (i) {
case 0:
NSLog(@"wtf");
int j = 1;
break;
}
I saw a few answers to this issue, and I get it — you can't declare and assign variables inside a switch
. But I'm wondering if the following is correct at throwing an error saying
error: expected expression before 'int'
Code:
switch (i) {
case 0:
int j = 1;
break;
}
Why would putting a call to NSLog()
before it result in no errors?
switch (i) {
case 0:
NSLog(@"wtf");
int j = 1;
break;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用的另一个简单的解决方法是在声明之前添加一个空表达式(分号)。 这可以避免将变量范围限制为代码块(或者某些 case 语句带有代码块,而另一些则不带)。
Another simple workaround I use is to add an empty expression (semicolon) before the declaration. This avoids limiting the variable scope to a code block (or having some case statements with code blocks and some without).
我以前遇到过这个问题,结论是你只需将代码放在一个块中即可。
I've run into this issue before, and the conclusion was that you just put the code inside a block.
如果您按照语言的语法进行操作,实际上可以在开关中声明变量。 您会收到错误,因为“
case 0:
”是一个标签,而在 C 中,将 声明 作为标签后的第一个语句是非法的 - 请注意编译器需要一个表达式,例如方法调用、正常赋值等。(尽管可能很奇怪,但这就是规则。)当您将 NSLog() 放在第一位时,您就避免了这个限制。 您可以将 case 的内容括在 { } 大括号中以引入作用域块,也可以将变量声明移到 switch 之外。 您选择哪个取决于个人喜好。 请注意,在 { } 大括号中声明的变量仅在该范围内有效,因此使用它的任何其他代码也必须出现在这些大括号内。
编辑:
顺便说一句,这种怪癖并不像您想象的那么罕见。 在 C 和 Java 中,在 for、while 或 中使用局部变量声明作为单独的语句(意思是“没有用大括号括起来”)也是非法的。 do 循环,甚至在 if 和 else 子句中(事实上,这在 "Java Puzzlers",我强烈推荐。)我认为我们通常不会一开始就写这样的错误,因为声明变量没有什么意义但是,对于 switch / case 结构中的唯一语句,有些人会省略大括号,因为 break 语句是关键语句。 始终
要查看编译器抛出的错误,请将这个可怕的、毫无意义的代码片段复制到您的 (Objective-)C 代码中:
使用 { } 大括号来分隔此类构造体的另一个原因:-)。
You actually can declare variables within a switch if you do it according to the syntax of the language. You're getting an error because "
case 0:
" is a label, and in C it's illegal to have a declaration as the first statement after a label — note that the compiler expects an expression, such as a method call, normal assignment, etc. (Bizarre though it may be, that's the rule.)When you put the NSLog() first, you avoided this limitation. You can enclose the contents of a case in { } braces to introduce a scoping block, or you can move the variable declaration outside the switch. Which you choose is a matter of personal preference. Just be aware that a variable declared in { } braces is only valid within that scope, so any other code that uses it must also appear within those braces.
Edit:
By the way, this quirk isn't as uncommon as you might think. In C and Java, it's also illegal to use a local variable declaration as the lone statement (meaning "not surrounded by braces) in a for, while, or do loop, or even in if and else clauses. (In fact, this is covered in puzzler #55 of "Java Puzzlers", which I highly recommend.) I think we generally don't write such errors to begin with because it makes little sense to declare a variable as the only statement in such contexts. With switch / case constructs, though, some people omit the braces since the break statement is the critical statement for control flow.
To see the compiler throw fits, copy this horrific, pointless snippet into your (Objective-)C code:
Yet another reason to always use { } braces to delimit the body of such constructs. :-)