iOS 枚舉的巧用

來源:互聯網
上載者:User

標籤:

前言

在之前的一篇文章中簡單的提到了這個問題, 但是自己寫的不詳細, 並且自己深入瞭解的也不是特別多, 在開發中也沒怎麼用到,所以經過閱讀者的反饋對這個問題很是疑惑! 本篇文章會分析之前的不足之處, 如果有地方不對還請幫忙糾正!

相關文章: iOS開發中你是否遇到這些經驗問題(二)

1.使用層面的理解

在這裡首先講如何簡單的使用, 僅僅是使用層面(有理解錯誤的地方幫忙糾正), 然後我們在去理解位元運算符! 在下面的圖中我們可以看見枚舉值中有<<(位元運算符:左移):


如果我們在枚舉值中看見<<那我們就可以通過|(位元運算符:或)進行組合使用如下代碼為例:

 //隨便添加一個UITextField UITextField *field = [UITextField new]; //Begin,Changed,DidEnd都能觸發UITextField的事件 [field addTarget:self action:@selector(textFieldDidChanged) forControlEvents: UIControlEventEditingDidBegin |                  UIControlEventValueChanged |                  UIControlEventEditingDidEnd     ]; [self.view addSubview:field];

如枚舉值中沒有<<,這就是普通的NSInteger類型的枚舉, 所以不能組合使用:


那蘋果官方是怎麼知道我們多個條件組合使用了呢? 答案是通過&(位元運算符:與)進行判斷的:

//controlEvents是組合使用後的一個值 NSUInteger controlEvents = UIControlEventEditingDidBegin | UIControlEventValueChanged | UIControlEventEditingDidEnd;    /**    //通過 & 來判斷是否包含:    UIControlEventEditingDidBegin,    UIControlEventValueChanged,    UIControlEventEditingDidEnd     */    if (controlEvents & UIControlEventEditingDidBegin) {        NSLog(@"UIControlEventEditingDidBegin");    }else if (controlEvents & UIControlEventValueChanged) {        NSLog(@"UIControlEventValueChanged");    }else if (controlEvents & UIControlEventEditingDidEnd) {        NSLog(@"UIControlEventEditingDidEnd");    }

那麼我們接下來看看使用過程中牽扯到的位元運算符, 我們會在下面舉個例子!

2.理解位元運算符

首先我們有一個枚舉, 下面代碼2種寫法我們暫時先不用管,等位元運算符講完我們會討論枚舉的宏使用:

//typedef NS_OPTIONS(NSInteger, myTests) {//    nameA = 1 << 0,//    nameB = 1 << 1,//    nameC = 1 << 2,//    nameD = 1 << 3,//};typedef enum {    nameA = 1 << 0,    nameB = 1 << 1,    nameC = 1 << 2,    nameD = 1 << 3,}myTests;/** nameA = 1 << 0 :值為1(2的0次方) nameB = 1 << 1 :值為2(2的1次方) nameC = 1 << 2 :值為4(2的2次方) nameD = 1 << 3 :值為8(2的3次方) */

通過&進行判斷我們來看看輸出結果如:


我們得到NSInteger value = nameA | nameB;的組合的值, 判斷結果是:1nameA的值, 2nameB的值, nameCnameD沒有組合使用所以值為0,最後我們知道如果value & nameC0說明value不包含nameC 相反則包含!

還有一點就是value & nameA就是nameA的值為1, value & nameB就是nameB的值為2

  • <<(左移):a << b就表示把a轉為二進位後左移b位(在後面添b0
  • |(或):只要有一個為1, 結果就是1
  • &(與):只要有二個為1, 結果才是1

我們已經知道nameA = 1, nameB = 2, nameC = 4, nameD = 8下面來通過二進位來解釋:

 NSInteger value = nameA | nameB | nameC | nameD;     轉成二進位:     nameA: 0 0 0 1       |     nameB: 0 0 1 0       |     nameC: 0 1 0 0       |     nameD: 1 0 0 0    ----------------     value: 1 1 1 1     上面是使用 | 得出value的值為1111(|的意思是有一個為1結果就為1)     下面是使用 & 判斷輸出的值(&的意思就是有二個為1結果才為1)      value: 1 1 1 1         value: 1 1 1 1        &                      &      nameA: 0 0 0 1         nameB: 0 0 1 0     ----------------       ----------------      結果值: 0 0 0 1         結果值: 0 0 1 0      我就寫2個例子:0001就是nameA的值, 0010就是nameB的值

相信大家已經明白其中的道理了, 接下來我們來看看枚舉的宏, 為了更好閱讀也可以看下面的:


3.枚舉的宏( NS_ENUMNS_OPTIONS)

NS_ENUMNS_OPTIONS宏提供了一個簡潔、定義枚舉和C語言選項的簡單方法。

The NS_ENUM and NS_OPTIONS macros provide a concise, simple way of defining enumerations and options in C-based languages. These macros improve code completion in Xcode and explicitly specify the type and size of your enumerations and options. Additionally, this syntax declares enums in a way that is evaluated correctly by older compilers, and by newer ones that can interpret the underlying type information.

這是最初的使用方法:

enum {        UITableViewCellStyleDefault,        UITableViewCellStyleValue1,        UITableViewCellStyleValue2,        UITableViewCellStyleSubtitle};typedef NSInteger UITableViewCellStyle;--------------------------------------------------enum {        UIViewAutoresizingNone                 = 0,        UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,        UIViewAutoresizingFlexibleWidth        = 1 << 1,        UIViewAutoresizingFlexibleRightMargin  = 1 << 2,        UIViewAutoresizingFlexibleTopMargin    = 1 << 3,        UIViewAutoresizingFlexibleHeight       = 1 << 4,        UIViewAutoresizingFlexibleBottomMargin = 1 << 5};typedef NSUInteger UIViewAutoresizing;

通過使用枚舉的宏:

NS_ENUM:是用來聲明一般的NSInteger(下面代碼使用NSInteger)類型的枚舉

Use the NS_ENUM macro to define enumerations, a set of values that are mutually exclusive.

NS_OPTIONS:是用來聲明位元遮罩(bitmasked)

Use the NS_OPTIONS macro to define options, a set of bitmasked values that may be combined together.

//NS_ENUMtypedef NS_ENUM(NSInteger, UITableViewCellStyle) {        UITableViewCellStyleDefault,        UITableViewCellStyleValue1,        UITableViewCellStyleValue2,        UITableViewCellStyleSubtitle};--------------------------------------------------//NS_OPTIONStypedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {        UIViewAutoresizingNone                 = 0,        UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,        UIViewAutoresizingFlexibleWidth        = 1 << 1,        UIViewAutoresizingFlexibleRightMargin  = 1 << 2,        UIViewAutoresizingFlexibleTopMargin    = 1 << 3,        UIViewAutoresizingFlexibleHeight       = 1 << 4,        UIViewAutoresizingFlexibleBottomMargin = 1 << 5};

NS_OPTIONSNS_ENUMenum 是有什麼區別呢?

1.通過上面介紹我們可以看出enum可以聲明一般類型和位元遮罩(bitmasked)類型

2.NS_ENUM聲明一般類型, NS_OPTIONS聲明掩碼(bitmasked)類型

3.那麼問題又來了, 直接用enum不就可以了? 答案不是這樣的, 蘋果建議我們在OC中使用NS_ENUMNS_OPTIONS, 為什麼呢? 因為他們除了推斷出不同類型的枚舉,再就是當編譯Objective-C++模式,它們產生的代碼是不同的, 就是因為不同所以混編的時候使用enum會報錯!



文/判若兩人丶(簡書作者)
原文連結:http://www.jianshu.com/p/97e582fe89f3
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。

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.