ios:關於枚舉

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   使用   ar   sp   div   

在電腦科學理論中,枚舉是一個被命名的整型常數的集合。定義形式如下:

enum 枚舉名{              標識符[=整型常數],              標識符[=整型常數],              ...              標識符[=整型常數],         } 枚舉變數; 
記憶體空間上,enum是枚舉型 union是共用體,成員共用一個變數緩衝區。它不參加記憶體的佔用和釋放,枚舉定義的變數可直接使用,甚至不用初始化。
如果枚舉沒有初始化,則從第一個標識符開始,順次賦給標識符0, 1, 2, ...。下面樣本枚舉中 n1,n2,n3,n4分別為0,1,2,3
enum NSInteger{n1, n2, n3, n4}x; 
但當枚舉中的某個成員賦值後,其後的成員按依次 加1的規則確定其值。
enum NSInteger{    n1,    n2 = 0,    n3 = 10,    n4}x; 
上面枚舉樣本中,n1,n2,n3,m4 分別為0,0,10,11。
1. 枚舉中每個成員(標識符)結束符是",", 不是";", 最後一個成員可省略 ","。
2. 初始化時可以賦負數,以後的標識符仍依次加1。
3. 枚舉變數只能取枚舉說明結構中的某個標識符常量,例如此時n4=11.

雖然枚舉類型被定義為一個整型集合,但是我測試還是可以定義成字串,聲明如下:
typedef enum : NSUInteger { blackColor, redColor, yellowColor } ThreeColor;
但是用法與整型枚舉有些區別,它不能像整型直接使用,直接輸出永遠是0,需要寫一個方法進行轉換你需要的字串。
- (NSString*) colorConvertToString:(ThreeColor) aColor {    NSString *result = nil;        switch(aColor) {        case blackColor:            result = @"black";            break;        case redColor:            result = @"red";            break;        case yellowColor:            result = @"yellow";            break;                    default:            result = @"unknown";    }        return result;}列印結果:NSLog(@"%@",[self colorConvertToString:redColor]);red
但一直覺得字串枚舉沒有什麼意義,從來沒有用過。

枚舉還支援位元運算,比如在UIView類裡面看到這樣的定義。
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {    UIViewAutoresizingNone                 = 0,    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,    UIViewAutoresizingFlexibleWidth        = 1 << 1,    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,    UIViewAutoresizingFlexibleHeight       = 1 << 4,    UIViewAutoresizingFlexibleBottomMargin = 1 << 5};
1<<0,0表示不進行移位 1還是1;大於1的會進行移位所以十進位的值分別是
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {    UIViewAutoresizingNone                 = 0,          //十進位,指定的位元,二進位    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,     //1,0,01    UIViewAutoresizingFlexibleWidth        = 1 << 1,     //2,1,10       轉換成 十進位  2    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,     //4,2,100      轉換成 十進位  4    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,     //8,3,1000     轉換成 十進位  8    UIViewAutoresizingFlexibleHeight       = 1 << 4,     //16,4,10000   轉換成 十進位  16    UIViewAutoresizingFlexibleBottomMargin = 1 << 5      //32,5,100000  轉換成 十進位  32};
位元運算的計算方式是將二進位轉換成十進位,也就是說,枚舉值裡面存取的是 計算後的十進位值.但是注意字串枚舉不適用於位元運算。

所以在代碼中下面這樣寫是沒有任何區別的。
    [UIButton buttonWithType:UIButtonTypeCustom];    [UIButton buttonWithType:0];
枚舉之間還可以這樣寫:(UIApplication.h)
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),};
有時候,常說枚舉一個數組,這裡枚舉其實是遍曆的意思。在ios中為我們提供了一個枚舉的類。(NSEnumerator.h)
@protocol NSFastEnumeration- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len;@end@interface NSEnumerator : NSObject <NSFastEnumeration>- (id)nextObject;@end@interface NSEnumerator (NSExtendedEnumerator)@property (readonly, copy) NSArray *allObjects;@end
集合類(如:NSArray、NSSet、NSDictionary等)均可擷取到NSEnumerator, 該類是一個抽象類別,沒有用來建立執行個體的公有介面。NSEnumerator的nextObject方法可以遍曆每個集合元素,結束返回nil,通過與while結合使用可遍曆集合中所有項。
樣本:
NSArray *anArray = // ... ;NSEnumerator *enumerator = [anArray objectEnumerator];id object;while ((object = [enumerator nextObject])) {    // do something with object...}
遍曆數組每個索引處的對象,你可以編寫一個0到[array count]的迴圈,而NSEnumerator用來描述這種集合迭代運算的方式。
通過objectEnumerator向數組請求列舉程式,如果想從後向前瀏覽集合,可使用reverseObjectEnumerator方法。在獲得列舉程式後,可以開始一個while迴圈,每次迴圈都向這個列舉程式請求它的下一個對象:nextObject。nextObject返回nil值時,迴圈結束。
也可以自訂列舉程式,要實現快速枚舉就必須實現NSFastEnumeration協議,也就是
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
並且覆蓋nextObject方法。
 

ios:關於枚舉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.