简体   繁体   中英

ObjC - NS_ENUM order?

Given a NS_ENUM enumeration macro as such:

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
    UITableViewCellStyleDefault,
    UITableViewCellStyleValue1,
    UITableViewCellStyleValue2,
    UITableViewCellStyleSubtitle
};

What is the order of the elements? I mean the underlying integer values of each element.

For example, does UITableViewCellStyleDefault have value 0, UITableViewCellStyleValue1 have value 1, UITableViewCellStyleValue2 have value 2 and UITableViewCellStyleSubtitle have value 3?

Or can it be something like: UITableViewCellStyleDefault has value 30, UITableViewCellStyleValue1 has value 15, UITableViewCellStyleValue2 has value 23 and UITableViewCellStyleSubtitle has value 3?

Thanks

NS_ENUM values start with 0 and increase sequentially. So in the case of the UITableViewCellStyle example, as it is coded there those values would be correct. There's generally no guarantee that the order of the enumeration won't change, so you should really consider those values to be opaque; certainly you shouldn't be comparing them or trying to do anything clever with math in most cases. If the actual values matter NS_OPTIONS would be a better option, but even with that the underlying value could change and that shouldn't really have any impact on code that relies on the option set.

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