Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.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 使用可选值创建枚举_Objective C - Fatal编程技术网

Objective c 使用可选值创建枚举

Objective c 使用可选值创建枚举,objective-c,Objective C,我正在使用一些代码,这些代码列出了使用枚举访问数组的部分,例如: typedef NS_ENUM(NSUInteger, ReportSection) { Section1, Section2, Section3, Count }; 但是,如果[self_test]的计算结果为true,我需要修改它以添加新行。条件是在运行时确定的,所以我认为我不能使用宏。生成的代码将是: typedef NS_ENUM(NSUInteger, ReportSection) {

我正在使用一些代码,这些代码列出了使用枚举访问数组的部分,例如:

typedef NS_ENUM(NSUInteger, ReportSection) {
    Section1,
    Section2,
    Section3,
    Count
};
但是,如果[self_test]的计算结果为true,我需要修改它以添加新行。条件是在运行时确定的,所以我认为我不能使用宏。生成的代码将是:

typedef NS_ENUM(NSUInteger, ReportSection) {
    Section1,
    Section2,
    NewRow // must be between section 2 and three
    Section3,
    Count
};
我正在使用现有的代码,所以修改应该保持在最低限度。如果我总是使用第二种情况,并且总是包含可选行,那么我只会得到一个错误

如果条件为真,有没有人知道如何做到这一点,而不必检查代码并增加枚举值?我知道这是可能的,但我希望有一个更优雅的解决方案存在


感谢您的帮助,如果这看起来很琐碎,我深表歉意。

不幸的是,objective-C不允许动态分配枚举。我有两个选择,但听起来都需要重构代码。将枚举中的值更改为属性,并在运行时分配它们,例如

@property NSUInteger Section1;
@property NSUInteger Section2;
....
或者使用相同的值创建为struct,在初始化包含可选newRow的任何结构时初始化该结构


您的代码不应该太脆弱,以至于依赖于枚举,如果需要任何修改,就会中断。把这当作是一个机会,让它变得更强大

为什么NewRow需要在Section2和Section3之间?只要确保您知道这一点,现有代码就会出现很多问题。这不应标记为c。枚举用于引用表示视图中行的数组中的值。行必须按特定顺序排列,newRow必须介于2和3之间,我知道@你是对的,我将删除它。你想要更动态的东西,而不是枚举。只需使用值数组。现有代码引用了无法更改的部分。我很担心。这会很烦人的。