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 是否可以在目标c中迭代屏蔽枚举_Objective C_Enums_Iteration - Fatal编程技术网

Objective c 是否可以在目标c中迭代屏蔽枚举

Objective c 是否可以在目标c中迭代屏蔽枚举,objective-c,enums,iteration,Objective C,Enums,Iteration,我知道这在其他语言中是可能的,但在objective-c中需要类似的东西 我有一个类似的枚举 typedef enum { option1 = 1 << 0, option2 = 1 << 1, option3 = 1 << 2 ... ... } SomePossibleOptions; 但我更愿意使用某种语法 foreach (option in myoption) { //do something } 有时,我在正常枚举中使用最后一个

我知道这在其他语言中是可能的,但在objective-c中需要类似的东西

我有一个类似的枚举

typedef enum {
  option1 = 1 << 0,
  option2 = 1 << 1,
  option3 = 1 << 2
...
...
} SomePossibleOptions;
但我更愿意使用某种语法

foreach (option in myoption)
{
  //do something
}

有时,我在正常枚举中使用最后一个值“SomeEnumCount”,该值正好包含枚举中的项数,因此我可以为此创建一个循环

在您的情况下,可能是这样的:

typedef enum {
  option1 = 1 << 0,
  option2 = 1 << 1,
  option3 = 1 << 2,
...
...
  optionCount = 1 << n
} SomePossibleOptions;

这是不可能的,因为在运行时,不再有值的枚举。。。只是一堆整数<代码>枚举仅为编译时的方便。
typedef enum {
  option1 = 1 << 0,
  option2 = 1 << 1,
  option3 = 1 << 2,
...
...
  optionCount = 1 << n
} SomePossibleOptions;
NSInteger optionsCount = (int)log2(optionCount);
for (NSInteger i = 0; i < optionsCount; i++) {
    SomePossibleOptions option = (SomePossibleOptions)(1 << i);

    //handle your options here
}
- (NSArray *)optionsInMask:(SomePossibleOptions)maskedOptions {
    NSMutableArray * options = [NSMutableArray array];

    NSInteger optionsCount = (int)log2(optionCount);
    for (NSInteger i = 0; i < optionsCount; i++) {
        SomePossibleOptions option = (SomePossibleOptions)(1 << i);
        if (maskedOptions & option) {
            [options addObject:[NSValue valueWithInteger:option]];
        }
    }

    return [NSArray arrayWithArray:options];
}
for (NSValue * value in [self optionsInMask:myOptions]) {
    SomePossibleOption option = (SomePossibleOptions)[value integerValue];

    //your code here
}