In the code (below) LINE_WIDTH
is declared a typedef
for enumeration where the names Large, Medium, Small and Hairline are int
co
In Swift you can use type double for enums
enum Constants: Double {
case π = 3.14159
case e = 2.71828
case φ = 1.61803398874
case λ = 1.30357
}
To know much about practical usage of enums in swift please refer https://appventure.me/2015/10/17/advanced-practical-enum-examples/
Objective-C doesn't support non-integer enum values. Your only option is to provide code that converts the enum value to a float
value.
One option is to create a simple function such as:
float LINE_WIDTH_float(LINE_WIDTH width);
Put that in the same .h as the enum declaration. Then add the following to some appropriate .m or .c file:
float LINE_WIDTH_float(LINE_WIDTH width) {
switch (width) {
case LINE_WIDTH_Large:
return 1.5f;
case LINE_WIDTH_Medium:
return 1.0f;
case LINE_WIDTH_Small:
return 0.5f;
case LINE_WIDTH_Hairline:
return 0.25f;
default:
return 0.0f;
}
}
So somewhere you have an enum value:
LINE_WIDTH someWidth = LINE_WIDTH_Medium;
float width = LINE_WIDTH_float(someWidth);
Another option would be to define an array of float
.
In the same .h as the enum add:
extern float *LINE_WIDTH_float;
Then in some appropriate .m or .c, you can add:
float *LINE_WIDTH_float = { 1.5, 1.0, 0.5, 0.25 };
Then to use this you can do:
LINE_WIDTH someWidth = LINE_WIDTH_Medium;
float width = LINE_WIDTH_float[someWidth];
Note that this is less safe than the first approach. It will likely crash if your enum value is set to something other than a valid enum value or if you add a new enum value but forget to update the values in the array.