ns_options ns_enum区别

NS_OPTIONS通常用来定义位移相关操做的枚举值,咱们能够参考UIKit.Framework的头文件,能够看到大量的枚举定义。url

typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {  spa

    UIViewAnimationTransitionNone,//默认从0开始  .net

    UIViewAnimationTransitionFlipFromLeft,  orm

    UIViewAnimationTransitionFlipFromRight,  blog

    UIViewAnimationTransitionCurlUp,  ip

    UIViewAnimationTransitionCurlDown,  文档

    };  it

  1.   

  2. typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {  io

  3.     UIViewAutoresizingNone                 = 0,  im

  4.     UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,  

  5.     UIViewAutoresizingFlexibleWidth        = 1 << 1,  

  6.     UIViewAutoresizingFlexibleRightMargin  = 1 << 2,  

  7.     UIViewAutoresizingFlexibleTopMargin    = 1 << 3,  

  8.     UIViewAutoresizingFlexibleHeight       = 1 << 4,  

  9.     UIViewAutoresizingFlexibleBottomMargin = 1 << 5  

  10. };  

这两个宏的定义在Foundation.framework的NSObjCRuntime.h中:

  1. #if (__cplusplus && __cplusplus >= 201103L && (__has_extension(cxx_strong_enums) || __has_feature(objc_fixed_enum))) || (!__cplusplus && __has_feature(objc_fixed_enum))  

  2. #define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type  

  3. #if (__cplusplus)  

  4. #define NS_OPTIONS(_type, _name) _type _name; enum : _type  

  5. #else  

  6. #define NS_OPTIONS(_type, _name) enum _name : _type _name; enum _name : _type  

  7. #endif  

  8. #else  

  9. #define NS_ENUM(_type, _name) _type _name; enum  

  10. #define NS_OPTIONS(_type, _name) _type _name; enum  

  11. #endif  


  1. typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {  

 展开获得:

  1. typedef enum UIViewAnimationTransition : NSInteger UIViewAnimationTransition;  

  2. enum UIViewAnimationTransition : NSInteger {  

从枚举定义来看,NS_ENUM和NS_OPTIONS本质是同样的,仅仅从字面上来区分其用途。NS_ENUM是通用状况,NS_OPTIONS通常用来定义具备位移操做或特色的状况(bitmask)。

实际使用时,能够直接定义:

  1. 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

相关文章
相关标签/搜索