objective-c 整数常量不会“减少为整数”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/554419/
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:08:20  来源:igfitidea点击:

integer constant does 'not reduce to an integer'

objective-c

提问by Dan Morgan

I use this code to set my constants

我使用此代码来设置我的常量

// Constants.h
extern NSInteger const KNameIndex;

// Constants.m
NSInteger const KNameIndex = 0;

And in a switch statement within a file that imports the Constant.h file I have this:

在导入 Constant.h 文件的文件中的 switch 语句中,我有这个:

switch (self.sectionFromParentTable) {
    case KNameIndex:
        self.types = self.facilityTypes;
        break;
    ...

I get error at compile that read this: "error:case label does not reduce to an integer constant"

我在编译时收到错误消息:“错误:案例标签不会减少为整数常量”

Any ideas what might be messed up?

任何想法可能会搞砸?

回答by epatel

For C/C++ and Objective-C must the case statement have fixed values - "reduced to an integer (read value)" at compile time

对于 C/C++ 和 Objective-C,case 语句必须具有固定值 -在编译时“减少为整数(读取值)

Your constants is not a real "constant" because it is a variable and I imagine it can be changed through a pointer - ie &KNameIndex

您的常量不是真正的“常量”,因为它是一个变量,我想它可以通过指针进行更改 - 即 &KNameIndex

Usually one defines constants as enum

通常将常量定义为 enum

enum {
    KNameIndex = 0,
    kAnotherConstant = 42
};

If you would use C++, or Objective-C++ (with .mm as file extension) you could use a const statement as

如果您将使用 C++ 或 Objective-C++(以 .mm 作为文件扩展名),您可以使用 const 语句作为

const int KNameIndex = 0;

回答by daniel

You can use

您可以使用

#define KNameIndex 0

...

switch (self.sectionFromParentTable) {
        case KNameIndex:
                self.types = self.facilityTypes;
                break;
        ...

and it should work.

它应该工作。

Just had the same problem and I decided to go with #define rather than enum. Works for me™ ;-)

刚刚遇到了同样的问题,我决定使用 #define 而不是 enum。对我来说有效™ ;-)

回答by Petriborg

This is a stab in the dark because I haven't used Cocoa / ObjC in a long time now, but is the member variable sectionFromParentTable not of int type?

这是黑暗中的刺,因为我已经很长时间没有使用 Cocoa / ObjC,但是成员变量 sectionFromParentTable 不是 int 类型吗?

回答by derobert

I have not worked with Objective C, but I'd try chucking the 'extern'. At least if this were C++, the Constants.mfile would not be part of the compilation unit of Other.m, so the value of KNameIndexwould be unknown to the compiler. Which would explain the error; an unknowable value can't be a constant.

我没有使用过 Objective C,但我会尝试放弃“extern”。至少如果这是 C++,该Constants.m文件将不是 的编译单元的一部分Other.m,因此KNameIndex编译器不知道的值。这将解释错误;一个不可知的值不能是一个常数。

Does putting the definition, not just the declaration, in the Constants.hfile help?

将定义而不只是声明放在Constants.h文件中是否有帮助?

回答by crashmstr

I think you are stuck with using a const intinstead of a const NSIntegeras switch only works with built in integral types. (not sure about your syntax with const flipped around after the type).

我认为您坚持使用 aconst int而不是 aconst NSInteger作为开关仅适用于内置的整数类型。(不确定你的语法在类型后翻转了 const )。

Take a look at the related question: Objective-C switch using objects?

看一下相关问题:Objective-C switch using objects?