简体   繁体   English

NS_ENUM提供有关前向引用的编译器警告

[英]NS_ENUM gives compiler warning about forward references

Im using the spiffy new NS_ENUM to try and define an enum in my objective-c iOS project. 我使用spiffy new NS_ENUM尝试在我的objective-c iOS项目中定义一个枚举。

I'm declaring the NS_ENUM in the header of a class like so: 我在类的标题中声明了NS_ENUM,如下所示:

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

@interface Issue : NSObject
....

And im getting the compiler warning: 我得到编译器警告:

ISO C forbids forward references to 'enum' types ISO C禁止前向引用“枚举”类型

Now if i define the enum the (slightly) older traditional way like so: 现在,如果我定义enum(稍微)旧的传统方式,如下所示:

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

@interface Issue : NSObject
....

in exactly the same place in the code the issue goes away. 在代码中完全相同的位置,问题就消失了。 What am i doing wrong with NS_ENUM? 我对NS_ENUM做错了什么?

EDIT: 编辑:

I corrected it by adding the typedef but its still giving a warning. 我通过添加typedef来纠正它,但它仍然发出警告。

I have turned on the pedantic compiler warnings. 我打开了迂腐的编译器警告。 Is this just a case where its being too pedantic or is there a correct way that im missing? 这只是一个过于迂腐的情况,还是有一种错误的正确方法?

Try: 尝试:

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

NS_ENUM won't do the typedef for you to declare the SomeEnumType type, you have to do it yourself. NS_ENUM不会为你声明SomeEnumType类型做typedef,你必须自己做。

Update: The reason why the warning shows up is due to the implementation of NS_ENUM. 更新:出现警告的原因是由于NS_ENUM的实现。 Let's see what it tries to do: 让我们看看它尝试做什么:

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

The problem line (I believe) is this: 问题(我相信)是这样的:

enum _name : _type _name;

This is performing a forward declaration within the macro itself. 这是在宏本身内执行前向声明。 Hence, with pedantic warnings, it's flagging the use of this up. 因此,随着迂腐的警告,它正在标志着这种使用。

The pedantic warning is simply stating if you wanted to transition this to pure C, it would not be portable as it does not follow the standardisation of no forward declarations of enums. 迂腐警告只是说明你是否想要将其转换为纯C,它不会是可移植的,因为它不遵循枚举的前向声明的标准化。 Within the realm of Xcode, Clang and LLVM (and the fact NS_ENUM is provided by Apple), you should be pretty safe. 在Xcode,Clang和LLVM(以及由Apple提供的NS_ENUM这一事实)的范围内,你应该非常安全。

You missed off the typedef : 你错过了typedef

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

You mentioned that you're using pedantic warnings. 你提到你正在使用迂腐警告。 The compiler is correct: the fixed-type enums are part of the C++ standard , not ISO C. 编译器是正确的:固定类型的枚举是C ++标准的一部分 ,而不是ISO C.

As others have pointed out, the pedantic warning is correct. 正如其他人所指出的那样,迂腐警告是正确的。 However, you don't have to use the NS_ENUM macro to take advantage of strictly typed enums. 但是,您不必使用NS_ENUM宏来利用严格类型的枚举。 Just declare your enum like this and the warning will go away while you retain the strict typing: 只需声明这样的枚举,当您保留严格的输入时警告就会消失:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM