iOS枚举

一、普通枚举

1、写法

typedef NS_ENUM (NSInteger, ColorType) {

      RedColor,
    
      BlackColor,
    
      WhiteColor
    
    };

NS_ENUM 标示;NSInteger 固定类型,不能为NSString其他类型;ColorType枚举的类型;ColorType 声明的变量只能为单个值

二、位移枚举

1、写法

typedef NS_OPTIONS (NSUInteger, NetWorkType)  {
    
    Unknown  =  0,         //      0
    
    NotReachable = 1 << 1,    // 10    2^1
    
    WiFi = 1 <<  2,      // 100       2^2
    
    WWAN  = 1 << 3       // 1000      2^3
    
};

NS_OPTIONS 标示;NSUInteger 固定类型;NetWorkType 枚举的类型;NetWorkType 声明的变量只能可以为多个值

2、例

    NetWorkType netWorkType=WiFi|WWAN;
    [self checkUserNetWorkState:netWorkType];

    -(void)checkUserNetWorkState:(NetWorkType )stateType{
    //进行&运算
    if (stateType&NotReachable) {
        NSLog(@"NotReachable");
    }
    if(stateType&WiFi){
        NSLog(@"WiFi");
    }
    if(stateType&WWAN){
        NSLog(@"WWAN");
    }
    }

输出:

    2017-06-15 20:50:23.754 BitOperation[1450:34567] WiFi
    2017-06-15 20:50:23.755 BitOperation[1450:34567] WWAN

3、iOS系统API例

typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
    UIView*view = [[UIView alloc]init];
    
    //view自动调整自己的宽度,保证与superView左边和右边的距离不变。自动调整自己的高度,保证与superView顶部和底部的距离不变
    view.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:view];

你可能感兴趣的:(iOS枚举)