ios - 表达式不是 iOS objective-c 中的整数常量表达式

标签 ios objective-c switch-statement

我想使用下面的表达式

-(void)SwitchCondn{
    int expression;
    int match1=0;
    int match2=1;

    switch (expression)

    {
        case match1:

            //statements

            break;

        case match2:

            //statements

            break;

        default:

           // statements

            break;

    }

但是我得到了

enter image description here

当我研究时我发现

In order to work in Objective-C, you should define your constant either like this:
#define TXT_NAME 1
Or even better, like this:
enum {TXT_NAME = 1};

我很长时间以来一直在使用这种方法。现在我的变量值将在运行时发生变化,所以我需要以其他方式定义,我不想使用 if else 所以有没有其他方式声明变量的方式

我已经学习了

Why can I not use my constant in the switch - case statement in Objective-C ? [error = Expression is not an integer constant expression]

Objective C switch statements and named integer constants

Objective C global constants with case/switch

integer constant does 'not reduce to an integer'

最佳答案

错误 expression is not an integer constant expression 的意思就是它所说的:在 case 中,值必须是常量,例如,不是变量。

您可以将 switch 上方的声明更改为常量:

const int match1=0;
const int match2=1;

或者您可以使用枚举。或者 #define。但是你不能在那里使用非常量变量。

关于ios - 表达式不是 iOS objective-c 中的整数常量表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31119606/

相关文章:

dart - Polymer Dart是否具有类似于Angular的ng-switch的功能?

c - 函数中的输入与 main 中的输入混合

java - 在字符串中使用 switch

ios - WKWebView canGoForward 总是返回 false swift 5

iOS Appdelegate 多个第 3 方依赖项初始化会减慢应用程序启动速度

ios - 当我们将应用程序重新提交到 App Store 时,我们是否应该更改可用日期?

objective-c - 如何将字符串从 Applescript 传递到 Objective C

html - iOS Safari 扩展框架集大于视口(viewport)

objective-c - Objective C - 从 NSString 到 Int 的类型转换

objective-c - Obj-C,在 iTunes 搜索查询字符串中编码简体中文?