iOS 定义枚举

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     winter
13 };
14 /**
15  你也从新定义对应的数字
16  */
17 typedef NS_ENUM(NSInteger, Sex) {
18     male = 1,
19     female = 0
20 };
21 
22 /**
23  这种不是很推荐.而且在使用的时候不能省略关键字:enum
24  */
25 enum MobilePhone{
26     iPhone,
27     android
28 };
29 
30 @end
31 
32 @implementation ViewController
33 
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

 

posted @ 2016-01-27 10:33  友朋  阅读(784)  评论(0编辑  收藏  举报