Sachin Sachin - 3 years ago 70
iOS Question

Enumerate NS_OPTIONS

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download