枚举

//普通枚举,写法1
typedef enum
{
    ZFDirectTypeLeft,
    ZFDirectTypeRight,
    ZFDirectTypeUp,
    ZFDirectTypeDown,
}ZFDirectType;

//普通枚举,写法2,参数(枚举值的类型,枚举的名称)
typedef NS_ENUM(NSInteger,ZFDirectType2)
{
    ZFDirectType2Left,
    ZFDirectType2Right,
    ZFDirectType2Up,
    ZFDirectType2Down,
};

//按位枚举,一次可以传多个值,参数(枚举值的类型,枚举的名称)
typedef NS_OPTIONS(NSInteger, ZFDirectType3)
{
    ZFDirectType3Left = 1 << 0,
    ZFDirectType3Right = 1 << 1 ,
    ZFDirectType3Up = 1 << 2,
    ZFDirectType3Down = 1 << 3,
};

- (void)viewDidLoad
{
    //给枚举传值可以传多个
    [self directCheck:ZFDirectType3Left | ZFDirectType3Up | ZFDirectType3Down | ZFDirectType3Right];
}

//按位与来判断传来的值都有哪些枚举值
- (void)directCheck:(ZFDirectType3)direct
{
    if(direct & ZFDirectType3Up)
    {
        NSLog(@"向上---%zd",direct & ZFDirectType3Up);
    }

    if(direct & ZFDirectType3Down)
    {
        NSLog(@"向下---%zd",direct & ZFDirectType3Down);
    }

    if(direct & ZFDirectType3Left)
    {
        NSLog(@"向左---%zd",direct & ZFDirectType3Left);
    }

    if(direct & ZFDirectType3Right)
    {
        NSLog(@"向右---%zd",direct & ZFDirectType3Right);
    }
}

results matching ""

    No results matching ""