• iOS: 枚举类型 enum,NS_ENUM,NS_OPTIONS


    转自:http://blog.csdn.net/annkie/article/details/9877643

    一般情况下,我们采用C风格的enum关键字可以定义枚举类型。

    [cpp] view plaincopy
     
    1. enum{   
    2.     UIViewAnimationTransitionNone,  
    3.     UIViewAnimationTransitionFlipFromLeft,  
    4.     UIViewAnimationTransitionFlipFromRight,  
    5.     UIViewAnimationTransitionCurlUp,  
    6.     UIViewAnimationTransitionCurlDown,  
    7. } UIViewAnimationTransition;  
    [cpp] view plaincopy
     
    1. //位移操作枚举定义  
    2. enum {  
    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. };  
    11. typedef NSUInteger UIViewAutoresizing;//使用NSUInteger的地方可以使用UIViewAutoresizing,//UIViewAutoresizing相当于NSUInteger的一个别名使用。  
    12. //因此一个UIViewAutoresizing的变量可以直接赋值给NSUInteger  

    枚举值一般是4个字节的int值,在64位系统上是8个字节。

    在iOS6和Mac OS 10.8以后Apple引入了两个宏来重新定义这两个枚举类型,实际上是将enum定义和typedef合二为一,并且采用不同的宏来从代码角度来区分。

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

    [cpp] view plaincopy
     
    1. typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {  
    2.     UIViewAnimationTransitionNone,//默认从0开始  
    3.     UIViewAnimationTransitionFlipFromLeft,  
    4.     UIViewAnimationTransitionFlipFromRight,  
    5.     UIViewAnimationTransitionCurlUp,  
    6.     UIViewAnimationTransitionCurlDown,  
    7. };  
    8.   
    9. typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {  
    10.     UIViewAutoresizingNone                 = 0,  
    11.     UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,  
    12.     UIViewAutoresizingFlexibleWidth        = 1 << 1,  
    13.     UIViewAutoresizingFlexibleRightMargin  = 1 << 2,  
    14.     UIViewAutoresizingFlexibleTopMargin    = 1 << 3,  
    15.     UIViewAutoresizingFlexibleHeight       = 1 << 4,  
    16.     UIViewAutoresizingFlexibleBottomMargin = 1 << 5  
    17. };  

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

    [cpp] view plaincopy
     
    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  

    [cpp] view plaincopy
     
    1. typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {  
     展开得到:
    [cpp] view plaincopy
     
    1. typedef enum UIViewAnimationTransition : NSInteger UIViewAnimationTransition;  
    2. enum UIViewAnimationTransition : NSInteger {  

    从枚举定义来看,NS_ENUM和NS_OPTIONS本质是一样的,仅仅从字面上来区分其用途。NS_ENUM是通用情况,NS_OPTIONS一般用来定义具有位移操作或特点的情况(bitmask)。

    实际使用时,可以直接定义:

    [cpp] view plaincopy
     
    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/

  • 相关阅读:
    【移动安全基础篇】——14、创建破解代码库
    【移动安全基础篇】——13、Android关键代码快速定位
    【移动安全基础篇】——12、Dalvik虚拟机
    【移动安全基础篇】——11、Android_jni
    【移动安全基础篇】——10、Android源代码修改
    【移动安全基础篇】——09、Android源代码目录结构
    【移动安全高级篇】————7、APK 的自我保护
    【移动安全高级篇】————6、Android DEX安全攻防战
    【移动安全高级篇】————5、Andorid APK反逆向解决方案---梆梆加固原理探寻
    Scene
  • 原文地址:https://www.cnblogs.com/wangpei/p/4105117.html
Copyright © 2020-2023  润新知