1

In the code (below) LINE_WIDTH is declared a typedef for enumeration where the names Large, Medium, Small and Hairline are int constants.

    typedef NS_ENUM(NSInteger, LINE_WIDTH)
    {
        LINE_WIDTH_Large,
        LINE_WIDTH_Medium,
        LINE_WIDTH_Small,
        LINE_WIDTH_Hairline    
    };

How can I use these int values to return float values of Large (1.5), Medium (1.0), Small (0.5) and Hairline (0.25) ?

NOTE: a simpler version of my earlier post that led no where despite this and this

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Greg
  • 1,750
  • 2
  • 29
  • 56
  • rmaddy, yes, but as the answer in the last link (above) shows the integer types can be used to access an array. And I cannot work out what I need to do to apply that here. – Greg May 30 '17 at 02:07
  • That answer doesn't apply here. That is for dealing with a range. I updated my answer with a 2nd option. The 1st option is still better. – rmaddy May 30 '17 at 02:46

2 Answers2

2

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.

CRD
  • 52,522
  • 5
  • 70
  • 86
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • rmaddy, your second option was where I was heading but I just finished testing your first option. It’s perfect! All four variables work in various classes throughout the project. e.g. `ring.layer.borderWidth = LINE_WIDTH_float(LINE_WIDTH_Large);` I'm starting to owe you quite a few beers! This has to be the accepted answer. – Greg May 30 '17 at 02:51
  • @CRD Thanks - too much Swift lately. – rmaddy May 30 '17 at 05:21
-1

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/

Anshad Rasheed
  • 2,526
  • 1
  • 14
  • 32