Objective-C 中枚举的迭代?

问题描述 投票:0回答:6

我有

enum Colour {
    white,
    pink,
    yellow,
    blue
} Colour;

我想做这样的事情:

for (int colour in Colour) {
    // Do something here.
}

我可以这样做吗?如果可以,怎么做?

objective-c
6个回答
155
投票

虽然问题已经得到解答,但我的两点意见是:

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);

我想这还不错,并且非常接近您想要的快速枚举。


18
投票

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

Type existingItem;
for ( existingItem in expression ) { statements }

表达式必须符合 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]);
}

13
投票
枚举中的

“Count”元素很好,但是除非您处理其中的“count”元素,否则它会在

switch
语句中显示“并非所有 switch 情况都已处理”。也许更好的方法是对第一个和最后一个元素使用别名:

enum Colour {
    firstColour = 0,

    white = firstColour,
    pink,
    yellow,
    blue,

    lastColour = blue
} Colour;

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

}

6
投票

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

我的颜色.h

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

static inline MYColorType MYColorTypeFirst() { return MYColorType0; }
static inline MYColorType MYColorTypeLast() { return MYColorType3; }

ViewController.m

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

值得注意的补充是 MYColorTypeFirst() 和 MYColorTypeLast() 的定义,它们在 for() 迭代中使用,放置在枚举定义附近以实现可维护性。


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];
    }

0
投票

这是使用预处理器避免弄脏编译代码的完美用例


在你的.h中:

typedef NS_ENUM(NSUInteger, EnumValue)
{
    EnumValueOne,
    EnumValueTwo,
    EnumValueThree,
    EnumValueFour
#define EnumValueLast EnumValueFour
};

应用程序中的其他地方:

for (EnumValue value = 0; value <= EnumValueLast; value++) {
    //do stuff
}
© www.soinside.com 2019 - 2024. All rights reserved.