加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 在switch语句中使用Obj-C对象时出现奇怪的编译器

发布时间:2020-12-16 09:30:37 所属栏目:百科 来源:网络整理
导读:在switch语句中使用Objective-C对象时出现编译器错误: switch (myConstant){ case 0: UIViewController *myController = [[[UIViewController alloc] init] autorelease]; [self.navigationController pushViewController:myViewController animated:YES];
在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;
}

错误说明:

Expected expression before ‘UIViewController’
‘myViewController’ undeclared (first use in this function)

我知道第二个错误是第一个错误的直接结果,但我不明白的是为什么我首先得到’预期表达式’错误…

如果我放一个;在案例结尾0:行,那么它将编译,但我不应该这样做,不是吗?

这也将无法编译,具有相同的错误:

switch (0)
{
    case 0:
        int a = 0;
        break;
    default:
        break;
}

但是,如果我声明int a;在开关块外面,则a = 0;编译好.

我以为我理解转换语句 – 显然我没有.有人可以解释一下吗?

解决方法

如果声明一个新变量,只需在案例中添加{}. switch语句的case部分不是在C中声明变量的正确范围.

case 0:
     {
        int a = 0;
        break;
    }
...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读