表达式不是 iOS 目标 c 中的整数常量表达式

编程入门 行业动态 更新时间:2024-10-15 04:21:16
本文介绍了表达式不是 iOS 目标 c 中的整数常量表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想用下面的表达

-(void)SwitchCondn{ int expression; int match1=0; int match2=1; switch (expression) { case match1: //statements break; case match2: //statements break; default: // statements break; }

但是我得到了

当我研究时发现

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 那么有没有其他方式声明变量的方法

I have been using this methods since long time . Now my variable value will change in run time so I need to define in others way and i didn't want to use if else so is there any way of declaration variable others way

我有以下学习

为什么我不能在 Objective-C 的 switch-case 语句中使用我的常量?[错误 = 表达式不是整数常量表达式]

Objective C switch 语句和命名整数常量

Objective C 全局常量与 case/switch

整数常量不会减少为整数"

推荐答案

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

The error expression is not an integer constant expression means just what it says: in a case, the value must be constant, as in, not a variable.

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

You could change the declarations above the switch to be constants:

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

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

Or you could use an enumeration. Or a #define. But you can't use non-constant variables there.

更多推荐

表达式不是 iOS 目标 c 中的整数常量表达式

本文发布于:2023-11-06 08:10:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563154.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   常量   整数   目标   iOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!