2016-05-31 28 views
0

此行是行,我的代码:NS_ENUM没有名字

typedef NS_ENUM(NSInteger, unused_name) 
{ k_a = 123, 
    k_b = 123, 
    k_c = 61 
}; 

但接下来的线都没有。编译器抱怨NS_ENUM的变量(...)部分不包含任何变量。

NS_ENUM(NSInteger) 
{ k_a = 123, 
    k_b = 123, 
    k_c = 61 
}; 

我和CF_ENUM有同样的问题。

我认为这不是头文件(Foundation/NSObjCRuntime.h或Core Foundation)的问题。

+0

我写了一个答案,但它看起来像你到目前为止一切正常。只要确保你的枚举声明在任何Objective-C接口或实现代码块之外。 – JAL

+0

不能有没有名字的NS_ENUM。该名称成为用于NS_ENUM的typedef的名称。使用诸如“unused_name”之类的非唯一名称会让您陷入困境。根本不给名称不起作用。这不是一个头的问题 - 这是你的代码的问题。你做错了事,编译器告诉你。 – gnasher729

+0

@ gnasher729除非我错过了一些东西,实际上你可以有一个没有名字的NS_ENUM(就像问题中的第二个代码块一样)。它只是在Xcode的代码完成中显示为enum 。 – JAL

回答

0

看在NSObjCRuntime.h头文件的注释:

/* NS_ENUM supports the use of one or two arguments. The first argument is always the integer type used for the values of the enum. The second argument is an optional type name for the macro. When specifying a type name, you must precede the macro with 'typedef' like so: 


typedef NS_ENUM(NSInteger, NSComparisonResult) { 
    ... 
}; 

If you do not specify a type name, do not use 'typedef'. For example: 

NS_ENUM(NSInteger) { 
    ... 
}; 
*/ 

总之,不要使用typedef如果你的枚举是无名的。另外,确保在任何Objective-C接口或实现块之外,在文件的顶部或底部声明枚举。

NS_ENUM(NSInteger) 
{ k_a = 123, 
    k_b = 123, 
    k_c = 61 
}; 

typedef NS_ENUM(NSInteger, unused_name) 
{ k_d = 123, 
    k_e = 123, 
    k_f = 61 
};