iOS 枚举是比较常用的结构. 枚举的变量是从0开始的NSInteger. 可以看做是一个#define .
列举常用的定义方式:
1 @interface ViewController () 2 /** 3 这种比较推荐,结构清晰,使用时可以省略关键字:enum .苹果官方推荐. 4 */ 5 typedef NS_OPTIONS(NSInteger, Season) { 6 /** 7 默认是从0开始 8 */ 9 spring,10 summer,11 autumn,12 winter13 };14 /**15 你也从新定义对应的数字16 */17 typedef NS_ENUM(NSInteger, Sex) {18 male = 1,19 female = 020 };21 22 /**23 这种不是很推荐.而且在使用的时候不能省略关键字:enum24 */25 enum MobilePhone{26 iPhone,27 android28 };29 30 @end31 32 @implementation ViewController33 34 - (void)viewDidLoad {35 [super viewDidLoad];36 // Do any additional setup after loading the view, typically from a nib.37 Season season = summer;38 NSLog(@"season=%ld",season);39 40 Sex sex = female;41 NSLog(@"sex=%ld",sex);42 43 enum MobilePhone mobilePhone = iPhone;44 NSLog(@"mobilePhone=%d",mobilePhone);45 46 }47 @end
原标题:iOS 定义枚举
关键词:IOS