typedef NS_ENUM vs typedef enum

后端 未结 2 1934
暗喜
暗喜 2021-02-07 10:48

On the Adopting Modern Objective-C guide, Apple recommends using the NS_ENUM macro instead of enum. I\'ve also read an explanation from NSHipster about NS_ENUM and NS_OPTIONS.

2条回答
  •  太阳男子
    2021-02-07 11:08

    NS_ENUM allows you to define a type. This means that the compiler can check if you're assigning the enum to a different variable like so:

    //OK in both cases
    NSInteger integer = SizeWidth;
    //OK only with typedef
    BOOL value = SizeHeight;
    

    NS_ENUM also provides checks in switch statements that you've covered all possible values:

    //Will generate warning if using NS_ENUM
    switch(sizeVariable) {
        case SizeWidth:
            //Do something
    }
    

提交回复
热议问题