Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have

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

and I would like to do something like this:

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

Can I do this and if yes, how? Thanks for your help!

share|improve this question
1  
possible duplicate of looping through enum values – Bo Persson Oct 2 '12 at 18:20
up vote 17 down vote accepted

an enum comes from C while fast enumeration was an addition of Objective-C 2.0.. they don't work together.

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

expression must conform to the NSFastEnumeration Protocol and be an Object! "elements" of an enum are not objects.

see this link for more information Apple's Fast Enumeration Documents

check this example to see how fast enumeration works:

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]);
}
share|improve this answer
    
Nice answer, thank you! – Matt N. Aug 2 '11 at 9:55
    
I don't but it will only let me do so after 5 min apparently ; ) – Matt N. Aug 2 '11 at 10:01
    
Ah yeah right, forgot about that :P never mind! ;) – MJB Aug 2 '11 at 10:03

Although the question is already answered, here are my two cents:

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

I guess it's not that bad and is pretty close to the fast enumeration you want.

share|improve this answer
    
That's in fact what I ended up implementing, thanks for posting it! – Matt N. Aug 2 '11 at 10:56
26  
you should accept this as the answer then... – SpaceDog Feb 13 '13 at 19:32
1  
So simple and yet so great ;) – ArkReversed Jun 27 '13 at 14:26
12  
Clever idea, but bad smell. Sneaking functionality into an identification mechanism like an enum breaks its intent and its cohesion. – dooleyo Aug 8 '13 at 7:52
3  
dooleyo, this kind of 'enumeration' is pretty typical for C/C++ coders. Of course, it works only for particular numeric enums. – Gobra Aug 8 '13 at 8:42

"Count" element in enum is nice, but it will get you "Not all switch cases were handled" in switch statement unless you handle this "count" element in it. Maybe a little better way is to use aliases for the first and for the last elements:

enum Colour {
    firstColour = 0,

    white = firstColour,
    pink,
    yellow,
    blue,

    lastColour = blue
} Colour;

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

}
share|improve this answer

I came to this post to answer this question as well. Gobra's answer is great. But my number of items may fluctuate, and correlate to a stored value, so to be extra safe that the "colorsCount" count is or was never a valid value, I ended up implementing the following and wanted to add to the discussion:

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

The notable addition being the definition of MYColorTypeFirst() and MYColorTypeLast(), which is used in the for() iteration, placed near the enum definition for maintainability.

share|improve this answer

For cases where I'm not the author of the enum, I do it like this. I think this is pretty future safe, as it doesn't cares about how the enum types are actually implemented.

    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];
    }
share|improve this answer
    
Don't you need to free that array when you're done..? – Jadar Jun 15 '15 at 14:06
    
That array is in the stack memory, it should get automatically purged when you leave the scope. – chunkyguy Jun 15 '15 at 16:33
    
What if I create it static and return it from a function? – Jadar Jun 15 '15 at 17:37
    
If it is static, it would live forever. Can you post some code, what you're trying to do? – chunkyguy Jun 15 '15 at 17:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.