Is there a way to assign values to enums during runtime in objective c? I have several enums and want each of the enum to have certain value. The values could be read from a xml file. Is there a way to do this?
Asked
Active
Viewed 1.5k times
2 Answers
17
Unfortunatley, @Binyamin is correct, you cannot do this with an enum. For this reason, I usually do the following in my projects:
// in .h
typedef int MyEnum;
struct {
MyEnum value1;
MyEnum value2;
MyEnum value3;
} MyEnumValues;
// in .m
__attribute__((constructor))
static void initMyEnum()
{
MyEnumValues.value1 = 10;
MyEnumValues.value2 = 75;
MyEnumValues.value3 = 46;
}
This also has the advantage of being able to iterate through the values, which is not possible with a normal enum:
int count = sizeof(MyEnumValues) / sizeof(MyEnum);
MyEnum *values = (MyEnum *) &MyEnumValues;
for (int i = 0; i < count; i++)
{
printf("Value %i is: %i\n", i, values[i]);
}
All in all, this is my preferred way to do enums in C.

Richard J. Ross III
- 55,009
- 24
- 135
- 201
-
@Richard..could you please explain what is the __attribute__((constructor)).. thank you for the answer though – i_raqz Apr 24 '12 at 20:30
-
1@learningDroid it's a GCC extension, which allows you to create a function that is called just before the target loads (in a dylib, it would be before the first function from the dylib is ran, in an application, it is just before `main()` ). A very useful construct, and as long as you do no heavy lifting in it, you should be fine. – Richard J. Ross III Apr 24 '12 at 20:32
-
I am planning to read an xml file and assign values to enums in a struct. Do you think this will be too heavy in the __attribute method – i_raqz Apr 24 '12 at 20:34
-
@learningDroid no, as long as the code takes less than 5 seconds to run. In which case, the OS will kill the application. – Richard J. Ross III Apr 24 '12 at 20:35
-
@learningDroid http://stackoverflow.com/questions/2053029/how-exactly-does-attribute-constructor-work – Richard J. Ross III Apr 24 '12 at 20:49
-
Hi, I tried your solution, but something is not working. Can you have a look? In .h: http://i49.tinypic.com/2j5ctxd.png In .m:http://i48.tinypic.com/p99pf.png – Luda Feb 20 '13 at 08:17
-
1@Luda Remove the keyword typedef before the struct keyword in your .h file – Dinesh Raja Feb 20 '13 at 09:00
-
@Luda R.A is correct. The typeset masks the struct itself, making the value unsettable. – Richard J. Ross III Feb 20 '13 at 09:09
-
Is it possible to explain in couple of words, why is it OK with struct and not with enum in objective-c? – Luda Feb 20 '13 at 09:58
-
as a follow up question.. is it possible to create an enum/struct without knowing before hand how many elements will be in it in run time? – abbood Mar 14 '13 at 07:10
-
@abbood Well, you could create a map or something somilar, but no, not with a struct or union. The closest you can get is C++ templates. A struct is a fixed layout of memory, and an enum is traditionally a fixed set of values. – Richard J. Ross III Mar 14 '13 at 11:19
-
This awesome! The only issue, for me, is that is not possible to use it as value for a `case` of a `switch` (because is not a constant expression)... :( – JP Illanes Apr 10 '15 at 03:05
6
No, enums information is erased at compile time.

MByD
- 135,866
- 28
- 264
- 277
-
5Unfortunately this is the case. However you can use a struct instead of an enum if you want to hold the information.. – Richard J. Ross III Apr 24 '12 at 20:16