简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM