Question

I have an NS_OPTIONS:

typedef NS_OPTIONS(NSUInteger, BrowserViewMenuOptions) {
    BrowserViewMenuOptionNone     = 0,
    BrowserViewMenuOptionCopy     = 1 << 0,
    BrowserViewMenuOptionMore     = 1 << 1,
    BrowserViewMenuOptionShare    = 1 << 2,
    BrowserViewMenuOptionDelete   = 1 << 3,
    BrowserViewMenuOptionDownload = 1 << 4,
};

Suppose I have a value like this:

(BrowserViewMenuOptionCopy | BrowserViewMenuOptionMore | BrowserViewMenuOptionShare)

How can I enumerate it like we do for an array?

Was it helpful?

Solution

You cannot enumerate as such, as these are constant values as opposed to elements in a collection, however if the enum follows a pattern without gaps, then you can generate all the numerical values of the enum. Yours does, so:

for (unsigned i = 0; i < 5; i++) {
    NSLog(@"value=%u", 1 << i);
}

In order to generate the names, you need a look-up table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top