3

Is there any way to use global int constants in Objective C that work in a case/switch statement? The technique here (http://stackoverflow.com/questions/538996/constants-in-objective-c) lets me access the constants everywhere, but does not let me put them into a switch statement.

in .h

FOUNDATION_EXPORT const int UNIT_IDLE;
FOUNDATION_EXPORT const int UNIT_DEFEND;

in .m

int const UNIT_IDLE = 0;
int const UNIT_DEFEND = 1;

Error is "Expression is not an integer constant expression"

Andrew
  • 341
  • 5
  • 16
  • what does your `switch` look like? – pb2q Jun 27 '12 at 21:45
  • possible duplicate of [integer constant does 'not reduce to an integer'](http://stackoverflow.com/questions/554419/integer-constant-does-not-reduce-to-an-integer) – zpasternack Jun 27 '12 at 23:27
  • I saw that solution, and I'm pretty new so I probably just don't understand, but I don't know how to access my enum in Object 1 in Object2. – Andrew Jun 28 '12 at 01:08
  • Possible duplicate of [Why can I not use my constant in the switch - case statement in Objective-C ? \[error = Expression is not an integer constant expression\]](http://stackoverflow.com/questions/6585276/why-can-i-not-use-my-constant-in-the-switch-case-statement-in-objective-c-e) – gypsicoder Nov 29 '16 at 08:50
  • Use `const static` instead. – Albert Renshaw Jun 29 '18 at 18:27

3 Answers3

5

I usually use enumerations with typedef statements when using constants which I will use in a switch statement.

For example, this would be in a shared .h file such as ProjectEnums.h:

enum my_custom_unit
{
    MyCustomUnitIdle    = 1,
    MyCustomUnitDefend  = 2
};
typedef enum my_custom_unit MyCustomUnit;

I can then use code similar to the following switch statement in my .c, .m, .cpp files:

#import "ProjectEnums.h"

- (void) useUnit:(MyCustomUnit)unit
{
    switch(unit)
    {
        case MyCustomUnitIdle:
        /* do something */
        break;

        case MyCustomUnitDefend:
        /* do something else */
        break;

        default:
        /* do some default thing for unknown unit */
        break;
    };
    return;
};

This also allows the compiler to verify the data being passed to the method and used within the switch statement at compile time.

David M. Syzdek
  • 15,360
  • 6
  • 30
  • 40
  • I'm a bit dense, but from another object can I have access to enum my_custom_unit? Or do I have to copy-paste the enum? I want to the constants shared. – Andrew Jun 28 '12 at 01:05
  • @Andrew You can declare the enums in a shared header file. I typically create a file called something like ProjectEnumerations.h which is included by any of the source files which will use the enums. I've updated my examples to demonstrate. – David M. Syzdek Jun 28 '12 at 01:40
  • Thanks so much! I was missing that last piece of how to share the enums. – Andrew Jun 28 '12 at 02:04
1

I think your best option is using enum types. Just declare a type in your header file and then you are ready to use it in a switch statement.

class.h

typedef enum{
    kEditGameModeNewGame = 0,
    kEditGameModeEdit = 1
}eEditGameMode;

class.m

eEditGameMode mode = kEditGameModeEdit;

switch (mode) {
    case kEditGameModeEdit:
        // ...
        break;
    case kEditGameModeNewGame:
        // ...
        break;

    default:
        break;
}

Good luck!

Xavi Gil
  • 11,460
  • 4
  • 56
  • 71
0

Official guideline says you should use "enumerations for groups of related constants that have integer values." That may solve your problem and improve code.

Mehdzor
  • 789
  • 4
  • 9
  • 23