在Objective C中对枚举进行迭代?

Mat*_* N. 42 objective-c

我有

enum Colour {
    white,
    pink,
    yellow,
    blue
} Colour;
Run Code Online (Sandbox Code Playgroud)

我想做这样的事情:

for (int colour in Colour){
    // Do something here.
}
Run Code Online (Sandbox Code Playgroud)

我可以这样做,如果是,怎么做?谢谢你的帮助!

Gob*_*bra 154

虽然问题已经得到解答,但这是我的两分钱:

enum Colour {
    white = 0,
    pink,
    yellow,
    blue,

    colorsCount // since we count from 0, this number will be blue+1 and will be actual 'colors count'
} Colour;

for (int i = 0; i < colorsCount; ++i)
  someFunc((Colour)i);
Run Code Online (Sandbox Code Playgroud)

我想这不是那么糟糕,而且非常接近你想要的快速枚举.

  • 聪明的主意,但难闻的气味.像enum这样的识别机制潜入功能会破坏其意图和凝聚力. (15认同)
  • dooleyo,这种"枚举"对于C/C++编码器来说非常典型.当然,它仅适用于特定的数字枚举. (3认同)

MJB*_*MJB 18

枚举来自C,而快速枚举是Objective-C 2.0的补充......它们不能一起工作.

Type existingItem;
for ( existingItem in expression ) { statements }
Run Code Online (Sandbox Code Playgroud)

表达式必须符合NSFastEnumeration协议并且是一个对象!枚举的"元素"不是对象.

有关详细信息,请参阅此链接Apple的快速枚举文档

查看此示例以查看枚举的工作速度:

NSArray *array = [NSArray arrayWithObjects:
        @"One", @"Two", @"Three", @"Four", nil];

for (NSString *element in array) {
    NSLog(@"element: %@", element);
}

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
    @"quattuor", @"four", @"quinque", @"five", @"sex", @"six", nil];

NSString *key;
for (key in dictionary) {
    NSLog(@"English: %@, Latin: %@", key, [dictionary objectForKey:key]);
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*kov 13

枚举中的"Count"元素很好,但switch除非你在其中处理这个"count"元素,否则它会在语句中找到"并非所有switch case都被处理" .也许更好的方法是为第一个和最后一个元素使用别名:

enum Colour {
    firstColour = 0,

    white = firstColour,
    pink,
    yellow,
    blue,

    lastColour = blue
} Colour;

for (int i = firstColour; i <= lastColour; ++i) {

}
Run Code Online (Sandbox Code Playgroud)

  • @OlSen 不,因为 `blue` == `lastColour` (2认同)

小智 6

我来这篇文章也回答了这个问题.Gobra的答案很棒.但是我的项目数量可能会波动,并且与存储的值相关联,因此为了更加安全,"colorsCount"计数是或从来不是有效值,我最终实现了以下内容并希望添加到讨论中:

MYColor.h

typedef NS_ENUM( NSInteger, MYColorType )
{
    MYColorType0 = 0,
    MYColorType1,
    MYColorType2,
    MYColorType3
};

static inline MYColorType MYColorTypeFirst() { return MYColorType0; }
static inline MYColorType MYColorTypeLast() { return MYColorType3; }
Run Code Online (Sandbox Code Playgroud)

ViewController.m

for ( int i = MYColorTypeFirst(); i <= MYColorTypeLast(); i++ )
{
    MYColor * color = [[MYColor alloc] initWithType:i];
    ...
}
Run Code Online (Sandbox Code Playgroud)

值得注意的是MYColorTypeFirst()和MYColorTypeLast()的定义,它在for()迭代中使用,放置在枚举定义附近以便于维护.


chu*_*guy 5

对于我不是枚举作者的情况,我会这样做。我认为这在未来是非常安全的,因为它不关心枚举类型是如何实际实现的。

    UISwipeGestureRecognizerDirection allDirections[] = {
        UISwipeGestureRecognizerDirectionDown,
        UISwipeGestureRecognizerDirectionLeft,
        UISwipeGestureRecognizerDirectionRight,
        UISwipeGestureRecognizerDirectionUp
    };

    for (int i = 0; i < sizeof(allDirections)/sizeof(allDirections[0]); ++i) {
        UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipe:)];
        swipeGesture.direction = allDirections[i];
        [self addGestureRecognizer:swipeGesture];
    }
Run Code Online (Sandbox Code Playgroud)