0

I have used typedef NS_ENUM to reorganise data constants in old code. Using an approach found here every typedef is declared in a single .h file that can be imported to any class in the project. Content of the .h file is wrapped in a message to the compiler. This works nicely for int variables.

MYCharacterType.h

    #ifndef MYCharacterType_h
    #define MYCharacterType_h 

    typedef NS_ENUM(NSInteger, MARGIN)
    {
        MARGIN_Top                          =  10,
        MARGIN_Side                         =  10,
        MARGIN_PanelBaseLine                =   1
    };
    ...
    #endif /* SatGamEnumType_h */

But Xcode complains when I try to include float variables

“Non-Integral type ’NSNumber’ is an invalid underlying type’

e.g.

    typedef NS_ENUM(NSNumber, LINE_WIDTH) {
        LINE_WIDTH_Large                    = 1.5,
        LINE_WIDTH_Medium                   = 1.0,
        LINE_WIDTH_Small                    = 0.5,
        LINE_WIDTH_Hairline                 = 0.25
    };

I get the same message whether I use NSValue or NSNumber so I suspect typedef NS_ENUM is not the way to define float variables (or at least the way I am using it).

The approach in this answer would only allow me to do what I have already organised in one file but does not offer a way to reorganise float variables in the same file. Could someone please explain how to do this so all variables are defined in one .h file regardless of their type ? Thanks

SOLUTION

This was answered by rmaddy after I approached the question differently.

Greg
  • 1,750
  • 2
  • 29
  • 56

1 Answers1

0

Defining different enums in one .h .. like just add it one file.

typedef NS_ENUM(NSInteger, MARGIN)
{
    MARGIN_Top                          =  10,
    MARGIN_Side                         =  10,
    MARGIN_PanelBaseLine                =   1
};


typedef NS_ENUM(long, ENUM_2)
{
    ENUM_2_1    = 10,
    ENUM_2_2    = 20,
    ENUM_2_3    = 30,
};

typedef NS_ENUM(long, ENUM_3)
{
    ENUM_3_1    = 10,
    ENUM_3_2    = 20,
    ENUM_3_3    = 30,
};

// And so on as many as you want

And your second question, Enums can only be of the integral data types like, int, long, long long, unsigned int, short etc... You can't use any Non-Integral types like float or double or not even any objective c types.

You can do enum mapping for float values like this https://stackoverflow.com/a/8867169/1825618

Bilal
  • 18,478
  • 8
  • 57
  • 72
  • Bilal, thanks for the link. I did see this answer previously but was unable to translate it into terms that might answer my question. The way I understand it, a typedef enum is used to find indices that access an array of floats. Should my float variables be arranged in order of magnitude (i.e. from 0.25 to 1.5 or vice versa) in order to I do this ? – Greg May 29 '17 at 07:49
  • If you don't assign any integer values to enum compiler automatically assigns from 0,1,2... And then yes it works as an array index. – Bilal May 29 '17 at 07:54
  • Bilal, I tried this but there are a few things I don't understand about the linked example. So I added an update to my initial question. You will probably spot what I am missing. – Greg May 29 '17 at 11:43