我看到了这个问题的一些答案,我明白了 - 你不能在一个内部声明和分配变量switch.但我想知道以下是否正确抛出错误说
错误:'int'之前的预期表达式
码:
switch (i) {
case 0:
int j = 1;
break;
}
Run Code Online (Sandbox Code Playgroud)
为什么NSLog()在它之前拨打电话会导致没有错误?
switch (i) {
case 0:
NSLog(@"wtf");
int j = 1;
break;
}
Run Code Online (Sandbox Code Playgroud) 切入追逐我已经重新创建了我的问题,因为它是相当自我解释的.
这符合,没有错误:
switch (n) {
case 1:
NSLog(@"");
NSString *aStr;
break;
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
这编译错误,它只缺少NSLog():
switch (n) {
case 1:
NSString *aStr;
break;
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
它在编译时出现错误"NSString'之前的预期表达式 "
我在这里错过了什么吗?
我想我会失明,因为我无法弄清楚此代码中语法错误的位置:
if( cell == nil ) {
titledCell = [ [ [ TitledCell alloc ] initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier ] autorelease
];
switch( cellNumber ) {
case 1:
NSString *viewDataKey = @"Name";
etc...
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,我在最后一行的'*'标记之前收到错误:语法错误.
对不起这个基本问题,但我错过了什么?
我想更多地了解" 为什么不能在switch语句中声明变量? "
我读了这篇文章,但我并没有完全理解.您可以在switch内部声明变量,但是要decalre并初始化变量或声明类的对象,它会给出complie time error.
请解释一下......
在switch语句中使用Objective-C对象时出现编译器错误:
switch (myConstant)
{
case 0:
UIViewController *myController = [[[UIViewController alloc] init] autorelease];
[self.navigationController pushViewController:myViewController animated:YES];
break;
case 1:
// stuff
break;
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
错误说明:
'UIViewController''myViewController'
未声明之前的预期表达式(首次在此函数中使用)
我知道第二个错误是第一个错误的直接结果,但我不明白的是为什么我首先得到'预期表达式'错误...
如果我;在行的末尾添加一个case 0:,那么它将编译,但我不应该这样做,不是吗?
这也将无法编译,具有相同的错误:
switch (0)
{
case 0:
int a = 0;
break;
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我声明int a;切换块的外部,那么a = 0;编译正常.
我以为我理解转换语句 - 显然我没有.有人可以解释一下吗?
我使用visual studi 2008.(c ++)
在我的开关案例中,我想创建一个对象,但我不能工作.
是不是,我不能在开关盒中创建一个对象?
如果这是对的,最好的解决方法是什么,
一个创建该对象的新方法?
编辑代码:
switch (causwahl){
case '1':
cAccount *oAccount = new cAccount (ID);
case '2' ....
Run Code Online (Sandbox Code Playgroud)