Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

integer constant does 'not reduce to an integer'

Tags:

objective-c

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:

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?

like image 754
Dan Morgan Avatar asked Feb 16 '09 20:02

Dan Morgan


People also ask

What does case label does not reduce to an integer constant mean?

In the switch case statement, a case can only have integral constant values i.e. integer or character type constant value. We cannot use any variable as case value. In this example, we are using case b: and b is a variable. Thus, error case label does not reduce to an integer constant occurs.

What does integer constant mean?

An integer constant is a value that is determined at compile time and cannot be changed at run time. An integer constant expression is an expression that is composed of constants and evaluated to a constant at compile time.

Is a constant An integer?

A constant (also called a literal) specifies a value. Constants are classified as string constants or numeric constants. String constants are further classified as character or graphic. Numeric constants are further classified as integer, floating point, or decimal.

What is not an integer constant?

Expert-verified answer32800 is not a valid integer constant of the type int. Explanation: There are 6 C constants. They are integer constants, octal-hexadecimal constants, real constants, character constants, backslash constants, string constants.


1 Answers

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

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

Usually one defines constants as 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

const int KNameIndex = 0;
like image 148
epatel Avatar answered Nov 11 '22 16:11

epatel