Change enum values at runtime?
13,490
Solution 1
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.
Solution 2
No, enums information is erased at compile time.
Author by
i_raqz
java programmer...sometimes work on android and iphone too
Updated on June 07, 2022Comments
-
i_raqz almost 2 years
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?
-
Richard J. Ross III about 12 yearsUnfortunately this is the case. However you can use a struct instead of an enum if you want to hold the information..
-
i_raqz about 12 years@Richard..could you please explain what is the __attribute__((constructor)).. thank you for the answer though
-
Richard J. Ross III about 12 years@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. -
i_raqz about 12 yearsI 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
-
Richard J. Ross III about 12 years@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 about 12 years@learningDroid stackoverflow.com/questions/2053029/…
-
Luda about 11 yearsHi, I tried your solution, but something is not working. Can you have a look? In .h: i49.tinypic.com/2j5ctxd.png In .m:i48.tinypic.com/p99pf.png
-
Dinesh Raja about 11 years@Luda Remove the keyword typedef before the struct keyword in your .h file
-
Richard J. Ross III about 11 years@Luda R.A is correct. The typeset masks the struct itself, making the value unsettable.
-
Luda about 11 yearsIs it possible to explain in couple of words, why is it OK with struct and not with enum in objective-c?
-
abbood about 11 yearsas 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?
-
Richard J. Ross III about 11 years@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.
-
JP Illanes about 9 yearsThis awesome! The only issue, for me, is that is not possible to use it as value for a
case
of aswitch
(because is not a constant expression)... :(