NS_ENUM gives compiler warning about direct references

Im using spiffy new NS_ENUM to try and define an enum in my objective-c iOS project.

I declare NS_ENUM in the header of a class like this:

NS_ENUM(int, SomeEnumType){
    SomeEnumType1,
    SomeEnumType2,
    SomeEnumType3,
    SomeEnumType4
};

@interface Issue : NSObject
....

      

And im getting a compiler warning:

ISO C forbids direct references to "enum" types

Now if I define an enum in the (slightly) older traditional way:

typedef enum{
    SomeEnumType1,
    SomeEnumType2,
    SomeEnumType3,
    SomeEnumType4
}SomeEnumType;

@interface Issue : NSObject
....

      

at exactly the same place in the code, the problem will go away. What am I doing wrong with NS_ENUM?

EDIT:

I fixed it by adding a typedef but still throwing a warning.

I have included pedal compiler warnings. Is this just a case of being overly pedantic or is there a correct path they are missing?

+3


source to share


3 answers


Try:

typedef NS_ENUM(int, SomeEnumType){
    SomeEnumType1,
    SomeEnumType2,
    SomeEnumType3,
    SomeEnumType4
};

      

NS_ENUM

will not introduce a typedef to declare a type SomeEnumType

, you have to do it yourself.

Update: The reason for the warning is related to the NS_ENUM implementation. See what he's trying to do:

#define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type

      



The problematic line (I believe) is this:

enum _name : _type _name;

      

This does the forward declaration inside the macro itself. Hence, with pedantic warnings, it indicates the use of it.

The pedantry warning simply states that if you want to translate it to pure C, it won't be portable, since it doesn't follow the standardization of forward branch declarations. Within Xcode, Clang and LLVM (and the fact that NS_ENUM is provided by Apple), you should be reasonably secure.

+6


source


You missed typedef

:

typedef NS_ENUM(int, SomeEnumType){
    SomeEnumType1,
    SomeEnumType2,
    SomeEnumType3,
    SomeEnumType4
};

      



You mentioned that you are using pedantic warnings. The compiler is correct: Fixed type enums are part of the C ++ standard , not ISO C.

+5


source


As others have pointed out, the pedantic warning is correct. However, you don't need to use a macro NS_ENUM

to use strongly typed enums. Just declare your enum this way and the warning goes away as long as you keep strict printing:

typedef enum : int {
    SomeEnumType1,
    SomeEnumType2,
    SomeEnumType3,
    SomeEnumType4
} SomeEnumType;

      

+3


source







All Articles