Objective-C / C giving enums default values

前端 未结 2 699
南旧
南旧 2021-02-06 00:32

I read somewhere about giving enums default values like so:

typedef enum  {
MarketNavigationTypeNone = 0,
MarketNavigationTypeHeirachy = 1,
MarketNavigationTypeM         


        
相关标签:
2条回答
  • 2021-02-06 01:21

    That are not default values, you are giving them the values they will always have.

    If you wouldn't initialize them explicitly, the first enumerators value is zero. For all others, if there is no initializer, their value is the value of the previous enumerator increased by one.

    There are two reasons for giving them explicit values:

    • you don't want them to have the values they'd have otherwise
    • you want to make it clear what value they have (for you or other developers)

    If you always refer to them by their name and never explicitly use an integral value for comparison or assignment, explicitly giving them a value is not needed.

    0 讨论(0)
  • 2021-02-06 01:24

    In general this only matters if the enum is exposed to some kind of external API or it is going to be used to exchange data via data files or other means. If the enum is only every used within your app and nowhere else then the actual values don't matter.

    0 讨论(0)
提交回复
热议问题