NS_OPTIONS通常用來定義位移相關操做的枚舉值,咱們能夠參考UIKit.Framework的頭文件,能夠看到大量的枚舉定義。url
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) { spa
UIViewAnimationTransitionNone,//默認從0開始 .net
UIViewAnimationTransitionFlipFromLeft, orm
UIViewAnimationTransitionFlipFromRight, blog
UIViewAnimationTransitionCurlUp, ip
UIViewAnimationTransitionCurlDown, 文檔
}; it
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) { io
UIViewAutoresizingNone = 0, im
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
這兩個宏的定義在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 {
從枚舉定義來看,NS_ENUM和NS_OPTIONS本質是同樣的,僅僅從字面上來區分其用途。NS_ENUM是通用狀況,NS_OPTIONS通常用來定義具備位移操做或特色的狀況(bitmask)。
實際使用時,能夠直接定義:
typedef enum : NSInteger {....} UIViewAnimationTransition;
等效於上述定義。
參考文檔:
1. http://nshipster.com/ns_enum-ns_options/
2.http://iamthewalr.us/blog/2012/11/ns_enum-and-ns_options/
原文:http://blog.csdn.net/annkie/article/details/9877643