相关疑难解决方法(0)

在switch语句中声明变量

我看到了这个问题的一些答案,我明白了 - 你不能在一个内部声明和分配变量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)

c variables declaration objective-c switch-statement

114
推荐指数
2
解决办法
3万
查看次数

我可以在Objective-C switch语句中声明变量吗?

我想我会失明,因为我无法弄清楚此代码中语法错误的位置:

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)

当我尝试编译它时,我在最后一行的'*'标记之前收到错误:语法错误.

对不起这个基本问题,但我错过了什么?

objective-c switch-statement

57
推荐指数
2
解决办法
2万
查看次数