iOS 一一 枚举类型

//
//  ViewController.m
//  Enum
//
//  Created by 朝阳 on 2017/12/15.
//  Copyright © 2017年 sunny. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%d,%d,%d,%d",Spring,Summer,Autumn,Winter);
    enum Season season = Spring;
    [self printSeason:season];
    
    NSLog(@"---------");
    Sex sex = MAN;
    [self printSex:sex];
    
    NSLog(@"---------");
    [self printDirection:ZYTypeTop];
    
    NSLog(@"---------");
    [self demo:ZYActionTypeTop | ZYActionTypeRight];
    
}

// 第一种: 普通枚举
enum Season{
    Spring = 0,
    Summer = 1,
    Autumn = 2,
    Winter = 3
};

- (void)printSeason:(enum Season)season
{
    switch (season) {
        case Spring:
            printf("春! \n");
            break;
        case Summer:
            printf("夏! \n");
            break;
        case Autumn:
            printf("秋! \n");
            break;
        case Winter:
            printf("冬! \n");
            break;
        default:
            break;
    }
}

// 第二种: 别名枚举
typedef enum{
    MAN,
    WOMAN,
    OTHER,
}Sex;

- (void)printSex:(Sex)sex
{
    switch (sex) {
        case MAN:
            printf("男! \n");
            break;
        case WOMAN:
            printf("女! \n");
            break;
        case OTHER:
            printf("不男不女! \n");
            break;
        default:
            break;
    }
}

// 第三种 typedf NS_ENUM 定义类型
typedef NS_ENUM(NSInteger,ZYType)
{
    ZYTypeTop,
    ZYTypeRight,
    ZYTypeBottom,
    ZYTypeLeft
};

- (void)printDirection:(ZYType)direction
{
    switch (direction) {
        case ZYTypeTop:
            printf("上! \n");
            break;
        case ZYTypeRight:
            printf("右! \n");
            break;
        case ZYTypeBottom:
            printf("下! \n");
            break;
        case ZYTypeLeft:
            printf("左! \n");
            break;
            
        default:
            break;
    }
}

// 第四种 位移枚举
// 一个参数可以传递多个值
// 注意: 当遇到位移枚举时,观察第一个枚举值,如果 !=0, 直接传0做参数即可,性能最高
typedef NS_OPTIONS(NSInteger, ZYActionType)
{
    ZYActionTypeTop = 1<<0, // 1 * 2(0) = 1
    ZYActionTypeRight = 1<<1, // 1 * 2(1) = 2
    ZYActionTypeBottom = 1<<2, // 1 * 2(2) = 4
    ZYActionTypeLeft = 1<<3, // 1 * 2(3) = 8
};

// 按位与 &  1&1==1  1&0==0  0&0==0; 只要有0则为0
// 按位或 |  1|1==1  1|0==1  0|0==0; 只要有1则为1
- (void)demo:(ZYActionType)type
{
    NSLog(@"type--%ld",type);
    
    if (type & ZYActionTypeTop) {
        NSLog(@"上---%ld",type & ZYActionTypeTop);
    }
    if (type & ZYActionTypeRight) {
        NSLog(@"右---%ld",type & ZYActionTypeRight);
    }
    if (type & ZYActionTypeBottom) {
        NSLog(@"下---%ld",type & ZYActionTypeBottom);
    }
    if (type & ZYActionTypeLeft) {
        NSLog(@"左---%ld",type & ZYActionTypeLeft);
    }
}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

white camel

感谢支持~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值