- //位移操做枚舉定義
- enum {
- UIViewAutoresizingNone = 0,
- UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
- UIViewAutoresizingFlexibleWidth = 1 << 1,
- UIViewAutoresizingFlexibleRightMargin = 1 << 2,
- UIViewAutoresizingFlexibleTopMargin = 1 << 3,
- UIViewAutoresizingFlexibleHeight = 1 << 4,
- UIViewAutoresizingFlexibleBottomMargin = 1 << 5
- };
- typedef NSUInteger UIViewAutoresizing;//使用NSUInteger的地方能夠使用UIViewAutoresizing,//UIViewAutoresizing至關於NSUInteger的一個別名使用。
- //所以一個UIViewAutoresizing的變量能夠直接賦值給NSUInteger
枚舉值通常是4個字節的int值,在64位系統上是8個字節。 app
在iOS6和Mac OS 10.8之後Apple引入了兩個宏來從新定義這兩個枚舉類型,其實是將enum定義和typedef合二爲一,而且採用不一樣的宏來從代碼角度來區分。
NS_OPTIONS通常用來定義位移相關操做的枚舉值,咱們能夠參考UIKit.Framework的頭文件,能夠看到大量的枚舉定義。 url
- typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
- UIViewAnimationTransitionNone,//默認從0開始
- UIViewAnimationTransitionFlipFromLeft,
- UIViewAnimationTransitionFlipFromRight,
- UIViewAnimationTransitionCurlUp,
- UIViewAnimationTransitionCurlDown,
- };
-
- 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
- };
這兩個宏的定義在Foundation.framework的NSObjCRuntime.h中: spa
- (__cplusplus && __cplusplus >= 201103L && (__has_extension(cxx_strong_enums) || __has_feature(objc_fixed_enum))) || (!__cplusplus && __has_feature(objc_fixed_enum))
- #define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
- #if (__cplusplus)
- #define NS_OPTIONS(_type, _name) _type _name; enum : _type
- #else
- #define NS_OPTIONS(_type, _name) enum _name : _type _name; enum _name : _type
- #endif
- #else
- #define NS_ENUM(_type, _name) _type _name; enum
- #define NS_OPTIONS(_type, _name) _type _name; enum
- #endif
- typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
展開獲得:
- typedef enum UIViewAnimationTransition : NSInteger UIViewAnimationTransition;
- enum UIViewAnimationTransition : NSInteger {
從枚舉定義來看,NS_ENUM和NS_OPTIONS本質是同樣的,僅僅從字面上來區分其用途。NS_ENUM是通用狀況,NS_OPTIONS通常用來定義具備位移操做或特色的狀況(bitmask)。 .net
實際使用時,能夠直接定義: blog
- typedef enum : NSInteger {....} UIViewAnimationTransition;
等效於上述定義。
參考文檔: ip
1. http://nshipster.com/ns_enum-ns_options/ 文檔
2.http://iamthewalr.us/blog/2012/11/ns_enum-and-ns_options/ get