【问题标题】:Error in using constant in the switch-case statement在 switch-case 语句中使用常量时出错
【发布时间】:2014-08-27 06:58:10
【问题描述】:

PRECAP: 我已关注Why can I not use my constant in the switch - case statement in Objective-C ? ,但问题仍然存在。此外,我不允许在上面的链接中评论我的问题,这就是我再次提出这个问题的原因。

在 iOS 应用程序中,我有 constants.h 为:

int const Category_Default = 1;

在另一个文件中(比如 test.m)我是这样使用的:

switch(//someVariable) {
    case Category_Default:
        //do something
    default:
        //do something
}

不幸的是,编译器给了我一个错误,因为 大小写标签不会减少为整数常量。

我知道必须在编译时知道常量的值才能在switch 内的case 中使用。

我什至按照Why can I not use my constant in the switch - case statement in Objective-C 中的建议初始化了 .h 文件本身中的常量,但错误仍然存​​在。

注意:在这种情况下,我不想使用#define 或enum。

对此的任何帮助将不胜感激。

【问题讨论】:

标签: objective-c switch-statement constants


【解决方案1】:

这是编译器规则我的朋友。您需要改用 enum 或 #define 。

case 标签在编译器可变时可以更改 编译器将 switch 语句视为一组 goto。这就是为什么它不能是变量

【讨论】:

  • 抱歉,我已经提到使用enum 或#define 的选项对我的情况无效,因为我无权更改constants.h 文件
  • 在这种情况下,您使用 if / else 语句。
猜你喜欢
  • 2016-03-25
  • 2016-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多