Foundation框架已經(jīng)為我們提供了更加“統(tǒng)一谴供、便捷”的枚舉定義方法
最好所有的枚舉都用“NS_ENUM”和“NS_OPTIONS”定義逸嘀,保證統(tǒng)一
在iOS6之后引入兩個(gè)宏來(lái)定義枚舉實(shí)際上是將enum定義和typedef合二為一扯俱,并且采用不同的宏來(lái)從代碼角度來(lái)區(qū)分蹋艺。
NS_ENUM猛蔽,定義狀態(tài)等普通枚舉
typedef NS_ENUM(NSUInteger, TTGState) {
TTGStateOK = 0,
TTGStateError,
TTGStateUnknow
};
NS_OPTIONS湖笨,定義選項(xiàng)
typedef NS_OPTIONS(NSUInteger, TTGDirection) {
TTGDirectionNone = 0,
TTGDirectionTop = 1 << 0,
TTGDirectionLeft = 1 << 1,
TTGDirectionRight = 1 << 2,
TTGDirectionBottom = 1 << 3
};
實(shí)現(xiàn)過(guò)程大概如下旗扑,通過(guò)位運(yùn)算組合判斷,這樣慈省,用位運(yùn)算臀防,就可以同時(shí)支持多個(gè)值。
//用“或”運(yùn)算同時(shí)賦值多個(gè)選項(xiàng)
TTGDirection direction = TTGDirectionTop | TTGDirectionLeft | TTGDirectionBottom;
//用“與”運(yùn)算取出對(duì)應(yīng)位
if (direction & TTGDirectionTop) {
NSLog(@"top");
}
if (direction & TTGDirectionLeft) {
NSLog(@"left");
}
if (direction & TTGDirectionRight) {
NSLog(@"right");
}
if (direction & TTGDirectionBottom) {
NSLog(@"bottom");
}
使用方式如下:
//隨便添加一個(gè)UITextField
UITextField *field = [UITextField new];
//Begin,Changed,DidEnd都能觸發(fā)UITextField的事件
[field addTarget:self action:@selector(textFieldDidChanged) forControlEvents: UIControlEventEditingDidBegin |
UIControlEventValueChanged |
UIControlEventEditingDidEnd
];
[self.view addSubview:field];
參考:
1边败、http://tutuge.me/2015/03/21/effective-objective-c-5-enum/?utm_source=tuicool&utm_medium=referral
2袱衷、http://www.reibang.com/p/97e582fe89f3