iOS 枚舉的巧用

前言

在以前的一篇文章中簡單的提到了這個問題, 可是本身寫的不詳細, 而且本身深刻了解的也不是特別多, 在開發中也沒怎麼用到,因此通過閱讀者的反饋對這個問題非常疑惑! 本篇文章會分析以前的不足之處, 若是有地方不對還請幫忙糾正!ide

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

1.使用層面的理解

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


若是咱們在枚舉值中看見<<那咱們就能夠經過|(位運算符:或)進行組合使用以下代碼爲例:lua

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

以下圖枚舉值中沒有<<,這就是普通的NSInteger類型的枚舉, 因此不能組合使用:spa


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

//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"); }

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

2.理解位運算符

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

//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次方) */

經過&進行判斷咱們來看看輸出結果以下圖:blog


咱們獲得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_ENUM typedef NS_ENUM(NSInteger, UITableViewCellStyle) { UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle }; -------------------------------------------------- //NS_OPTIONS 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 };

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 著做權歸做者全部,轉載請聯繫做者得到受權,並標註「簡書做者」。
相關文章
相關標籤/搜索