zoukankan      html  css  js  c++  java
  • iOS

    前言

    • iOS 5.0 之后,提供了新的枚举定义方式,定义枚举的同时,可以指定枚举中数据的类型。

      	typedef NS_OPTIONS(_type, _name) new;   -> 位移的,可以使用 按位或 设置数值
      	typedef NS_ENUM(_type, _name) new;      -> 数字的,直接使用枚举设置数值
      
    • 位移型枚举:

      • 使用 按位或 可以给一个参数同时设置多个 "类型"。在具体执行的时候,使用 按位与 可以判断具体的 "类型"。
      • OC 中 64 位操作系统 NSInteger 64 位 - long => 能够表示 64 种选项。通过位移设置,就能够得到非常多的组合。
      • 对于位移枚举类型,如果传入 0,表示什么附加操作都不做!=> 执行效率是最高的。如果开发中,看到位移的枚举,同时不要做任何的附加操作,参数可以直接输入 0!

    1、枚举的定义

    1.1 C 样式枚举定义

    • 定义枚举类型

      	/*
          	typedef enum new;
       
          	new:枚举类型的变量值列表
       
          	C 样式的枚举默认枚举类型变量值的格式为整型
       	*/
      
      	typedef enum {
          
          	AA,
          	BB,
          	CC
          
      	} Name;
      
    • 判断枚举值

          - (void)studentWithName:(Name)name {
              
              switch (name) {
                      
                  case 0:
                      
                      NSLog(@"AA");
                      break;
                      
                  case 1:
                      
                      NSLog(@"BB");
                      break;
                      
                  case 2:
                      
                      NSLog(@"CC");
                      break;
                      
                  default:
                      break;
              }
          }
      
    • 设置枚举的值

      	[self studentWithName:1];
              
        	[self studentWithName:CC];
      

    1.2 数字型枚举定义

    • 定义枚举类型

      	/*
      		typedef NS_ENUM(_type, _name) new;                      
           
      		_type:枚举类型变量值的格式
      		_name:枚举类型的名字
      		new:枚举类型的变量值列表
      	*/
      
      	typedef NS_ENUM(NSUInteger, Seasons) {
              
      		spring = 0,
      		summer,
      		autumn,
      		winter
      	};
      
    • 判断枚举值

      	- (void)selectWithSeasons:(Seasons)seasons {
              
      		if (seasons == 1 || seasons == 2) {
                  
      			NSLog(@"comfortable");
                  
      		} else {
                  
      			NSLog(@"cold");
      		}
      	}
      
    • 设置枚举的值

      	[self selectWithSeasons:0];
              
      	[self selectWithSeasons:autumn];
      

    1.3 位移型枚举定义

    • 定义枚举类型

      	/*
      		typedef NS_OPTIONS(_type, _name) new;
           
      		_type:枚举类型变量值的格式
      		_name:枚举类型的名字
      		new:枚举类型的变量值列表
           
      		位移的枚举判断不能使用 else,否则会丢选项
      	*/
      
      	typedef NS_OPTIONS(NSUInteger, ActionTypeOptions) {
              
      		ActionTypeTop =     1 << 0,
      		ActionTypeBottom =  1 << 1,
      		ActionTypeLeft =    1 << 2,
      		ActionTypeRight =   1 << 3
      	};
      
    • 判断枚举值

      	- (void)movedWithActionType:(ActionTypeOptions)type {
              
      		if (type == 0) {
      			return;
      		}
              
      		if (type & ActionTypeTop) {
      			NSLog(@"上 %li", type & ActionTypeTop);
      		}
      		if (type & ActionTypeBottom) {
      			NSLog(@"下 %li", type & ActionTypeBottom);
      		}
      		if (type & ActionTypeLeft) {
      			NSLog(@"左 %li", type & ActionTypeLeft);
      		}
      		if (type & ActionTypeRight) {
      			NSLog(@"右 %li", type & ActionTypeRight);
      		}
      	}
      
    • 设置枚举的值

      	[self movedWithActionType:0];
                                  
      	[self movedWithActionType:ActionTypeLeft | ActionTypeTop | ActionTypeBottom | ActionTypeRight];
      
  • 相关阅读:
    多线程 NSThread GCD
    UICollectionView 集合视图用法,自定义Cell
    简单的注册 登录
    数据库sqlite的使用
    沙盒机制 归档 反归档
    图片异步加载 ,KVO
    iOS开发系列--通知与消息机制
    制作iOS Ad-Hoc测试应用
    NT_iOS笔记—判断iPhone6
    iOS开发多线程篇—单例模式(ARC)
  • 原文地址:https://www.cnblogs.com/QianChia/p/5780953.html
Copyright © 2011-2022 走看看