博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ns_options ns_enum区别
阅读量:7024 次
发布时间:2019-06-28

本文共 1820 字,大约阅读时间需要 6 分钟。

hot3.png

NS_OPTIONS一般用来定义位移相关操作的枚举值,我们可以参考UIKit.Framework的头文件,可以看到大量的枚举定义。

typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {  

    UIViewAnimationTransitionNone,//默认从0开始  

    UIViewAnimationTransitionFlipFromLeft,  

    UIViewAnimationTransitionFlipFromRight,  

    UIViewAnimationTransitionCurlUp,  

    UIViewAnimationTransitionCurlDown,  

    };  

  1.   

  2. typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {  

  3.     UIViewAutoresizingNone                 = 0,  

  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

转载于:https://my.oschina.net/u/2418942/blog/528602

你可能感兴趣的文章
Kernel Trace System
查看>>
linux文件系统详解
查看>>
我的友情链接
查看>>
ibatis mybatis sql语句配置 符号不兼容 大于号 小于号
查看>>
Alipay 开源 SofaRPC
查看>>
jquery的extend与fn.extend
查看>>
自动化测试应该学什么
查看>>
语音识别对比分析
查看>>
Linux命令之 wc
查看>>
在virtualbox中安装的ubuntu系统 共享数据空间分配的数据
查看>>
WinRAR4.11激活
查看>>
Oracle常用单行函数
查看>>
运行浏览器的最高版本
查看>>
PHP中解决ajax请求session过期退出登录问题
查看>>
拆分功能:第一步,建立 会员管理系统
查看>>
我的友情链接
查看>>
CXF+Spring+Hibernate实现RESTful webservice服务端示例
查看>>
HTML网络效果收集---HTML
查看>>
css @语法,@规则 @import @charset @font-face @fontdef @media @page
查看>>
asp.net系统过滤器、自定义过滤器
查看>>