在switch语句中使用的OBJ-C对象时,奇编译器错误 [英] Odd compiler error when using Obj-C objects in a switch statement
问题描述
我用switch语句中一个Objective-C对象时得到一个编译错误:
I get a compiler error when using an Objective-C object within a switch statement:
switch (myConstant)
{
case 0:
UIViewController *myController = [[[UIViewController alloc] init] autorelease];
[self.navigationController pushViewController:myViewController animated:YES];
break;
case 1:
// stuff
break;
default:
break;
}
该错误提示:
在UIViewController的结果预计前pression
myViewController'未申报(在一次使用此功能)
Expected expression before 'UIViewController'
'myViewController' undeclared (first use in this function)
据我所知,第二个错误是第一个错误的直接结果,但我不明白的是,为什么我得到的预期前pression错误摆在首位...
I understand that the second error is a direct result of the first error, but what I don't understand is why I get the 'expected expression' error in the first place...
如果我把一个;
在年底 0的情况:
行,那么它会编译,但我不应该这样做,不是吗?
If I put a ;
at the end of the case 0:
line, then it will compile, but I shouldn't have to do this, no?
这也将无法编译,具有相同的错误:
This will also fail to compile, with the same error:
switch (0)
{
case 0:
int a = 0;
break;
default:
break;
}
不过,如果我宣布了 int类型的;
开关块外,那么 A = 0;
编译罚款。
However, if I declare the int a;
outside of the switch block, then a = 0;
compiles fine.
我想我明白switch语句 - 显然我不知道。可能有人请解释一下吗?
I thought I understood switch statements - clearly I don't. Could someone please explain?
推荐答案
只需添加{}的情况下内如果声明一个新的变量。 switch语句的情况下,部分不是一个正确的范围,在C声明变量。
Just add {} within the case if you declare a new variable. The case part of a switch statement is not a correct scope to declare variables in C.
case 0:
{
int a = 0;
break;
}
...
这篇关于在switch语句中使用的OBJ-C对象时,奇编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!