在switch语句中定义块会导致编译器错误 [英] Defining a block in a switch statement results in a compiler error

查看:117
本文介绍了在switch语句中定义块会导致编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下switch语句:

Consider the following switch statement:

switch (buttonIndex) {
    case 0:
        [self fooWithCompletion:^{
            [weakSelf finishEditing];
        }];
        break;
    case 1: // Error here
        [self barWithCompletion:^{
            [weakSelf finishEditing];
        }];
        break;    
    default:
        break;
}

它会导致编译器错误

Cannot jump from switch statement to this case label

Cannot jump from switch statement to this case label

case 1:行上.

为什么会发生这种情况,我该如何解决?

Why is this happening and how do I fix it?

推荐答案

块定义创建了一个新范围,该范围似乎干扰了编译器正确解释switch语句的能力.

The block definition creates a new scope which seems to interfere with the compiler's ability to correctly interpret the switch statement.

为每个案例标签添加范围定界符可解决该错误.我认为这是因为该块的作用域现在无疑是大小写作用域的子代.

Adding scope delimiters for each case label resolves the error. I think this is because the block's scope is now unambiguously a child of the case scope.

switch (buttonIndex) {
    case 0:
    {
        [self updateUserDataWithCompletion:^{
            [weakSelf finishEditing];
        }];
        break;
    }
    case 1:
    {
        [self updateOtherDataWithCompletion:^{
            [weakSelf finishEditing];
        }];
        break;
    }
    default:
        break;
}

对于类似的问题,还有一个使用LLVM打开的错误.

There's a bug open with LLVM for a similar issue.

这篇关于在switch语句中定义块会导致编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆