关于enum与typedef enum的用法小结

感谢原作者:http://blog.youkuaiyun.com/annkie/article/details/9877643

枚举类型,是列举出几个常用数据类型中的一种

它在OC中的定义如下

typedef enum{

UIViewShowBySevralPage 

UIViewShowByOnePage,

UIViewShowBySevralSection

}UITableViewShowtype;


使用如下:

为了方便阅读,养成良好的习惯,应当避免在代码中临时定义常量

应该多用枚举类型

@interface frashDemoViewController :UIViewController {

UITableView  *showView;

NSMutableArray   *datasouce;

UITableViewShowtype _showStyle;

}

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

[cpp]  view plain copy
  1. enum{   
  2.     UIViewAnimationTransitionNone,  
  3.     UIViewAnimationTransitionFlipFromLeft,  
  4.     UIViewAnimationTransitionFlipFromRight,  
  5.     UIViewAnimationTransitionCurlUp,  
  6.     UIViewAnimationTransitionCurlDown,  
  7. } UIViewAnimationTransition;  
[cpp]  view plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  1. typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {  
 展开得到:
[cpp]  view plain copy
  1. typedef enum UIViewAnimationTransition : NSInteger UIViewAnimationTransition;  
  2. enum UIViewAnimationTransition : NSInteger {  

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

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

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


<think>好的,我现在需要回答用户关于C语言中typedef enumtypedef struct的区别及用法比较的问题。首先,我得回忆一下这两个概念的基本知识,然后结合提供的引用内容来组织答案。 首先,typedef enumtypedef struct都是用来定义新类型的,但它们分别对应枚举和结构体。我需要明确两者的用途、语法和实际应用中的不同。 根据引用[1],typedef的作用是给现有类型起别名,所以无论是enum还是struct,加上typedef后都可以用新名字来声明变量。比如,typedef enum { ... } MyEnum; 之后,可以直接用MyEnum声明变量,而不需要每次都写enum关键字。 接下来,关于enum和struct本身的区别。枚举(enum)用于定义一组命名的整数常量,而结构体(struct)则是将不同类型的数据组合成一个整体。例如,enum可能用来表示状态码,而struct用来表示一个复杂的数据结构,比如学生信息。 引用[3]提到,typedef可以为内部数据类型和自定义类型(如struct)定义别名。这说明无论是enum还是struct,都可以通过typedef简化声明。例如,typedef struct { ... } MyStruct; 这样声明后,可以直接用MyStruct来定义变量,而不需要写struct关键字。 然后,我需要比较两者的语法和使用场景。枚举类型通常用于有限的、固定的值集合,比如星期、颜色等。而结构体用于组合多个变量,这些变量可以是不同的类型,比如一个学生的姓名、年龄和成绩。 在用法上,typedef enum的例子可能像这样: typedef enum { RED, GREEN, BLUE } Color; Color c = RED; 而typedef struct的例子: typedef struct { int age; char *name; } Person; Person p = {20, "Alice"}; 用户可能还会关心内存分配和访问方式的不同。结构体中的每个成员有独立的内存空间,而枚举的每个成员是整数值,共享同一个类型。 还需要注意,没有typedef的情况下,声明enum变量需要enum关键字,而struct也需要struct关键字。但使用typedef后,这些关键字可以省略,使得代码更简洁。 最后,结合引用[2],可能提到typedef和宏定义的区别,但用户的问题主要在于enum和struct,所以这部分可能不需要深入,除非用户后续提问。 总结起来,回答的结构应该是先介绍typedef的作用,再分别说明typedef enumtypedef struct的用途、语法示例,最后比较两者的区别,包括用途、存储内容和声明方式的不同。</think>### typedef enumtypedef struct的区别及用法比较 #### 1. **核心概念** - **typedef enum**:用于定义**枚举类型**,枚举是一组**命名的整数常量**,常用于表示有限的状态或选项。例如: ```c typedef enum { MONDAY, TUESDAY, WEDNESDAY } Weekday; // 定义枚举类型别名Weekday ``` 使用时可直接声明变量:`Weekday day = MONDAY;`。 - **typedef struct**:用于定义**结构体类型**,结构体是**多个不同类型数据的集合**,常用于封装复杂数据。例如: ```c typedef struct { int id; char name[20]; } Student; // 定义结构体类型别名Student ``` 使用时直接声明:`Student s = {1, "Alice"};`。 #### 2. **语法对比** | 特性 | typedef enum | typedef struct | |---------------------|--------------------------------------------|---------------------------------------------| | **声明方式** | `typedef enum { 成员列表 } 别名;` | `typedef struct { 成员列表 } 别名;` | | **成员类型** | 仅限整数常量(隐式赋值或显式赋值) | 任意类型(int、float、指针等) | | **内存占用** | 单个整型大小(通常4字节) | 各成员内存总和(可能存在对齐填充) | | **典型用途** | 状态码、选项集合(如颜色、星期) | 数据聚合(如学生信息、坐标点) | #### 3. **关键区别** - **数据组织方式**: - **enum**:所有枚举常量共享同一内存空间,本质是整数值的别名。 - **struct**:每个成员独立存储,可通过`.`操作符访问。 - **类型安全**: - 枚举类型(C11后)支持强类型检查,直接赋值整数值可能报错(需强制转换)[^3]。 - 结构体成员需严格按类型访问,但整体无类似限制。 #### 4. **应用示例** - **枚举类型**:定义错误码 ```c typedef enum { SUCCESS = 0, FILE_NOT_FOUND = 1, PERMISSION_DENIED = 2 } ErrorCode; ``` 调用时可直接用`ErrorCode`代替`int`,增强可读性。 - **结构体类型**:定义几何图形 ```c typedef struct { float x; float y; } Point; typedef struct { Point center; float radius; } Circle; ``` 通过嵌套结构体实现复杂数据建模。 #### 5. **联合使用场景** 两者可结合使用,例如用枚举标记结构体的类型: ```c typedef enum { CIRCLE, RECTANGLE } ShapeType; typedef struct { ShapeType type; union { Circle circle; Rectangle rect; } data; } Shape; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值