0

PRECAP: I have followed Why can I not use my constant in the switch - case statement in Objective-C ? but still the problem persists. Also I am not allowed to comment my question in the above link and that is why I put this question again.

In a iOS app, I have constants.h as:

int const Category_Default = 1;

In another file (say test.m) I used it in this way:

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

Unfortunately the compiler gives me an error as Case label does not reduce to an integer constant.

I am aware that the value of a constant must be known at compile time to be able to be used in a case within a switch.

I even initialized the constant in the .h file itself as suggested in Why can I not use my constant in the switch - case statement in Objective-C but still the error persists.

NOTE: I don't want to use #define or enum in this case.

Any help on this would be much appreciated.

Community
  • 1
  • 1
Sibir
  • 313
  • 2
  • 6
  • 20
  • 1
    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) – Shai Aug 27 '14 at 06:59
  • 1
    @Shai Yes it is a duplicate but I already mentioned that I followed it and yet the problem persists!! – Sibir Aug 27 '14 at 07:02

1 Answers1

0

It's Compiler rule my friend. You need to use enum or #define instead.

case labels can change when they are variable as per compiler the compiler considers a switch statement as a set of gotos. that's why it can't be variables

Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88
  • Sorry but I already mentioned that this option of using `enum` or `#define` isn't valid on my case as I am not authorised to change the `constants.h` file – Sibir Aug 27 '14 at 07:11
  • In that case you use an if / else statement. – gnasher729 Aug 27 '14 at 08:01