參考:http://blog.csdn.net/annkie/article/details/9877643
一般情況下暑中,我們采用C風(fēng)格的enum關(guān)鍵字可以定義枚舉類型:
enum{
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
} UIViewAnimationTransition;
//位移操作枚舉定義
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相當(dāng)于NSUInteger的一個(gè)別名使用。
//因此一個(gè)UIViewAutoresizing的變量可以直接賦值給NSUInteger
枚舉值一般是4個(gè)字節(jié)的int值割粮,在64位系統(tǒng)上是8個(gè)字節(jié)。
在iOS6和Mac OS 10.8以后Apple引入了兩個(gè)宏來(lái)重新定義這兩個(gè)枚舉類型刊咳,實(shí)際上是將enum定義和typedef合二為一秘遏,并且采用不同的宏來(lái)從代碼角度來(lái)區(qū)分。
NS_OPTIONS一般用來(lái)定義位移相關(guān)操作的枚舉值鹿寻,我們可以參考UIKit.Framework的頭文件睦柴,可以看到大量的枚舉定義。
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
UIViewAnimationTransitionNone,//默認(rèn)從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
};
這兩個(gè)宏的定義在Foundation.framework的NSObjCRuntime.h中:
#if (__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 {
從枚舉定義來(lái)看毡熏,NS_ENUM和NS_OPTIONS本質(zhì)是一樣的坦敌,僅僅從字面上來(lái)區(qū)分其用途。NS_ENUM是通用情況,NS_OPTIONS一般用來(lái)定義具有位移操作或特點(diǎn)的情況(bitmask)狱窘。
實(shí)際使用時(shí)杜顺,可以直接定義:
typedef enum : NSInteger {....} UIViewAnimationTransition;
等效于上述定義。