- //位移操做枚举定义
- 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