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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1115304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:54:07  来源:igfitidea点击:

Can I declare variables inside an Objective-C switch statement?

objective-cswitch-statement

提问by JoBu1324

I think I'm going blind, because I can't figure out where the syntax error is in this code:

我想我要瞎了,因为我无法弄清楚这段代码中的语法错误在哪里:

if( cell == nil ) {
    titledCell = [ [ [ TitledCell alloc ] initWithFrame:CGRectZero
        reuseIdentifier:CellIdentifier ] autorelease
    ];

    switch( cellNumber ) {
        case 1:
            NSString *viewDataKey = @"Name";
etc...

When I try to compile it, I'm getting an Error: syntax error before '*' tokenon the last line.

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

Sorry for such a basic question, but what am I missing?

抱歉问这么基本的问题,但我错过了什么?

回答by ephemient

I don't have a suitable Objective-C compiler on hand, but as long as the C constructs are identical:

我手头没有合适的 Objective-C 编译器,但只要 C 结构相同:

switch { … }gives you oneblock-level scope, not one for each case. Declaring a variable anywhere other than the beginning of the scope is illegal, and inside a switchis especiallydangerous because its initialization may be jumped over.

switch { … }为您提供一个块级作用域,而不是每个case. 声明比任何地方范围的开始以外的变量是非法的,里面switch特别危险的,因为它的初始化,可以跳过。

Do either of the following resolve the issue?

以下任一操作是否可以解决问题?

NSString *viewDataKey;
switch (cellNumber) {
    case 1:
        viewDataKey = @"Name";
    …
}

switch (cellNumber) {
    case 1: {
        NSString *viewDataKey = @"Name";
        …
    }
    …
}

回答by Chuck

You can't declare a variable at the beginning of a case statement. Make a test case that just consists of that and you'll get the same error.

不能在 case 语句的开头声明变量。制作一个只包含它的测试用例,你会得到同样的错误。

It doesn't have to do with variables being declared in the middle of a block — even adopting a standard that allows that won't make GCC accept a declaration at the beginning of a case statement. It appears that GCC views the case label as part of the line and thus won't allow a declaration there.

它与在块中间声明的变量无关——即使采用允许不会让 GCC 在 case 语句开头接受声明的标准。似乎 GCC 将 case 标签视为行的一部分,因此不允许在那里声明。

A simple workaround is just to put a semicolon at the beginning of the case so the declaration is not at the start.

一个简单的解决方法是在案例的开头放一个分号,这样声明就不会在开头。

回答by Aragorn

In C you only can declare variables at the begining of a block before any non-declare statements.

在 C 中,您只能在块的开头在任何非声明语句之前声明变量。

{
   /* you can declare variables here */

   /* block statements */

   /* You can't declare variables here */
}

In C++ you can declare variables any where you need them.

在 C++ 中,您可以在任何需要的地方声明变量。

回答by Phil Miller

Might it not be valid to declare a variable within a switch block?

在 switch 块中声明变量可能无效吗?

回答by egarlock

You can create a variable within a switch statement but you will have to create it within a block so that the scope of that variable is defined.

您可以在 switch 语句中创建变量,但必须在块中创建它,以便定义该变量的范围。

Example:

例子:

switch(number){

    case 1:
        {
            // Create object here
            // object is defined only for the scope of this block
        }
        break;
    case 2:
        {
            // etc.
        }
        break;
    default:
        break;

}

回答by lin zheng

How to solve the warning:

如何解决警告:

1.Insert one ;in the first line of your case block

1.;在案例块的第一行插入一个

2.Put codes inside braces

2.将代码放在大括号内