Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c ObjC-NS_枚举顺序?_Objective C_Macros_Enums - Fatal编程技术网

Objective c ObjC-NS_枚举顺序?

Objective c ObjC-NS_枚举顺序?,objective-c,macros,enums,Objective C,Macros,Enums,给定NS_枚举宏,如下所示: typedef NS_ENUM(NSInteger, UITableViewCellStyle) { UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle }; 元素的顺序是什么?我指的是每个元素的基本整数值 例如,UITableViewCellStyleDef

给定NS_枚举宏,如下所示:

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
    UITableViewCellStyleDefault,
    UITableViewCellStyleValue1,
    UITableViewCellStyleValue2,
    UITableViewCellStyleSubtitle
};
元素的顺序是什么?我指的是每个元素的基本整数值

例如,UITableViewCellStyleDefault的值是否为0,UITableViewCellStyleValue1的值是否为1,UITableViewCellStyleValue2的值是否为2,UITableViewCellStyleSubtitle的值是否为3

或者可以是这样的:UITableViewCellStyleDefault的值为30,UITableViewCellStyleValue1的值为15,UITableViewCellStyleValue2的值为23,UITableViewCellStyleSubtitle的值为3


谢谢

NS_ENUM
值从0开始依次递增。因此,在
UITableViewCellStyle
示例中,这些值是正确的。通常不能保证枚举的顺序不会改变,所以您应该真正地考虑这些值是不透明的;当然,在大多数情况下,你不应该比较它们,也不应该尝试用数学做任何聪明的事情。如果实际值很重要,
NS\u OPTIONS
将是一个更好的选项,但即使这样,基础值也可能会发生变化,这对依赖于选项集的代码不会产生任何影响

使用NS_ENUM时,它们是从0开始的顺序。NS_选项将用于提供任意值。值得注意的是,使用NS_ENUM时,该值实际上并不重要。如果您的代码依赖于某个特定值,则可能是其他问题的代码气味。^这是正确的。你应该把它变成一个答案
typedef NS_ENUM(NSInteger,UITableViewCellStyle){…}
typedef enum:Integer{…}UITableViewCellStyle相同
这就是
NS_ENUM
所做的。