Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
I thought to be smart and define a sets of constants, just like Apple.
e.g. NSTableViewNoColumnAutoresizing

.h file:
extern int const myConstant;
.m file:
int const myConstant = 9;

So I figured I could use these in a switch

swith ([delegate mySwitch]) {
case myConstant:
break;
}

But this gives a compile error. Is this normal and I can not do things in this manner, or did I do something wrong?

error: case label does not reduce to integer constant
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
I thought to be smart and define a sets of constants, just like Apple.
e.g. NSTableViewNoColumnAutoresizing

.h file:
extern int const myConstant;
.m file:
int const myConstant = 9;

So I figured I could use these in a switch

swith ([delegate mySwitch]) {
case myConstant:
break;
}

But this gives a compile error. Is this normal and I can not do things in this manner, or did I do something wrong?

error: case label does not reduce to integer constant

It's normal. You are using Objective-C, which is based on C, not on C++. In the C language, const variables are still variables and don't count as "constant expressions".

Usually your best choice is to use an "enum" to define a number of related constants; slightly less good is to use a series of #define preprocessor statements.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
.h file:
extern int const myConstant;
.m file:
int const myConstant = 9;

I don't think this would work even in C++ in general anyway. It works if the .m file containing the switch can see the value of myConstant but if it only sees the .h file, ie myConstant really is an extern, then the compiler would throw a similar error. I think you're much better off using enums or #defines like gnasher729 suggests.

b e n
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.