ios enum NS_ENUM,NS_OPTIONS

作者: sttech | 来源:发表于2017-07-04 16:49 被阅读0次

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

enum{   
    UIViewAnimationTransitionNone,  
    UIViewAnimationTransitionFlipFromLeft,  
    UIViewAnimationTransitionFlipFromRight,  
    UIViewAnimationTransitionCurlUp,  
    UIViewAnimationTransitionCurlDown,  
}

位移动操作枚举定义

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个字节

typedef NS_ENUM(NSInteger, UIBarButtonItemStyle) {
    UIBarButtonItemStylePlain,// 默认从0开始
    UIBarButtonItemStyleBordered,
    UIBarButtonItemStyleDone,
};
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  
}; 

这两个宏的定义在framework的NSObjCRuntime中

定义

从枚举定义来看,NS_ENUM和NS_OPTIONS本质是一样的。NS_ENUM是通用情况,NS_OPTIONS一般用来定义具有位移操作或特点的情况

相关文章

网友评论

    本文标题:ios enum NS_ENUM,NS_OPTIONS

    本文链接:https://www.haomeiwen.com/subject/xodxhxtx.html