NS_ENUM和NS_OPTIONS宏

枚举的宏定义 

一、简介  
NS_ENUM和NS_OPTIONS宏提供了一种简明、简单的方法来定义基于C语言的枚举和选项。 这些宏提高了Xcode中的代码完成性,并明确指定了枚举和选项的类型和大小。 此外,此语法以一种方式来声明枚举,该方式由旧编译器正确地计算,并且由更新的编译器来解释底层类型信息。 使用NS_ENUM、NS_OPTIONS宏定义枚举,有助于定义枚举的名称和类型; 
如果需要以按位或操作来组合的枚举都应该使用NS_OPTIONS宏;若枚举不需要互相组合,可以使用NS_ENUM来定义 
二、例子 
(1)如果使用枚举定义UITableViewCellStyle,可以使用NS_ENUM宏来改写 
enum{ 
UITableViewCellStyleDefault, 
UITableViewCellStyleValue1, 
UITableViewCellStyleValue2, 
UITableViewCellStyleSubtitle, 
}; 
typedef NSInteger UITableViewCellStyle; 
改写版 
typedef NS_ENUM(NSInteger,UITableViewCellStyle){

UITableViewCellStyleDefault,
  UITableViewCellStyleValue1,
  UITableViewCellStyleValue2,
  UITableViewCellStyleSubtitle,

}

(2)使用枚举定义的的一组位掩码。可以使用NS_OPTIONS来改写 
enum{

UIViewAutoresizingNone                         =0,
UIViewAutoresizingFlexibleLeftMargin     =1<<0,
UIViewAutoresizingFlexibleWidth             =1<<1,
UIViewAutoresizingFlexibleRightMargin   =1<<2,
……


使用NS_OPTIONS来改写 
typedef NS_OPTIONS(NSInteger,UIViewAutoresizing){

UIViewAutoresizingNone                         =0,
UIViewAutoresizingFlexibleLeftMargin     =1<<0,
UIViewAutoresizingFlexibleWidth             =1<<1,
UIViewAutoresizingFlexibleRightMargin   =1<<2,
……

}

原文地址:https://www.cnblogs.com/feng9exe/p/10083578.html