您当前的位置: 首页 >  ios

white camel

暂无认证

  • 3浏览

    0关注

    442博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

iOS 一一 枚举类型

white camel 发布时间:2017-12-15 13:33:23 ,浏览量:3

//
//  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 *)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            
关注
打赏
1661428283
查看更多评论
0.0387s