iOS - OC Enum 枚舉


前言

  • 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];
    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM