李洪强iOS开发之 - enum与typedef enum的用法
2016-09-24 09:32
323 查看
[b]李洪强iOS开发之 - enum与typedef enum的用法[/b]
[b]01 - 定义枚举类型[/b]
[b]
[/b]
上面我们就在ViewController.h定义了一个枚举类型,[b]枚举类型的值默认是连续的自然数,例如例子中的[/b]
TO_BE_PAID=0,//开始
[b] 那么其后的就依次为1,2,3....所以一般只需要设置枚举中第一个的值就可以。[/b]
注意: 在定义枚举类型的时候一定要定义在.h中的#imort 和€interface之间定义,位置不能错了
02 - 定义操作类型
[b]enum和enum typedef 在IOS中的使用[/b]
[b]第一、typedef的使用[/b]
C语言里typedef的解释是用来声明新的类型名来代替已有的类型名,typedef为C语言的关键字,作用是为一种数据类型定义一个新名字。这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等)
如:typedef char gender;
gender a;与char a;语句相同。
[b]第二 、enum的使用[/b]
enum是枚举类型, enum用来定义一系列宏定义常量区别用,相当于一系列的#define xx xx,当然它后面的标识符也可当作一个类型标识符。
如:
enum AlertTableSections
{
kUIAction_Simple_Section = 1,
kUIAction_OKCancel_Section,
kUIAction_Custom_Section,
kUIAlert_Simple_Section,
kUIAlert_OKCancel_Section,
kUIAlert_Custom_Section,
};
kUIAction_OKCancel_Section的值为2.
第三、typedef enum 的使用
typedef enum则是用来定义一个数据类型,那么该类型的变量值只能在enum定义的范围内取。
typedef enum {
UIButtonTypeCustom = 0, // no button type
UIButtonTypeRoundedRect, // rounded rect, flat white button, like in address card
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
} UIButtonType;
UIButtonType表示一个类别,它的值只能是UIButtonTypeCustom....
在了解enum和typedef enum的区别之前先应该明白typedef的用法和意义。
C语言里typedef的解释是用来声明新的类型名来代替已有的类姓名,例如:
typedef int CHANGE;
指定了用CHANGE代表int类型,CHANGE代表int,那么:
int a,b;和CHANGE a,b;是等价的、一样的。
方便了个人习惯,熟悉的人用CHANGE来定义int。
typedef为C语言的关键字,作用是为一种数据类型定义一个新名字。这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等)。
而enum是枚举类型,有了typedef的理解容易看出,typedef enum定义了枚举类型,类型变量取值在enum{}范围内取,在使用中二者无差别。
enum AlertTableSections
{
kUIAction_Simple_Section = 0,
kUIAction_OKCancel_Section,
kUIAction_Custom_Section,
kUIAlert_Simple_Section,
kUIAlert_OKCancel_Section,
kUIAlert_Custom_Section,
};
typedef enum {
UIButtonTypeCustom = 0, // no button type
UIButtonTypeRoundedRect, // rounded rect, flat white button, like in address card
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
} UIButtonType;
看上面两个例子更好理解,下面的是UIButton的API,UIButtonType指定的按钮的类型,清楚名了,上面的直接调用enum里的元素就可以了。
[b]01 - 定义枚举类型[/b]
[b]
[/b]
上面我们就在ViewController.h定义了一个枚举类型,[b]枚举类型的值默认是连续的自然数,例如例子中的[/b]
TO_BE_PAID=0,//开始
[b] 那么其后的就依次为1,2,3....所以一般只需要设置枚举中第一个的值就可以。[/b]
注意: 在定义枚举类型的时候一定要定义在.h中的#imort 和€interface之间定义,位置不能错了
02 - 定义操作类型
[b]enum和enum typedef 在IOS中的使用[/b]
[b]第一、typedef的使用[/b]
C语言里typedef的解释是用来声明新的类型名来代替已有的类型名,typedef为C语言的关键字,作用是为一种数据类型定义一个新名字。这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等)
如:typedef char gender;
gender a;与char a;语句相同。
[b]第二 、enum的使用[/b]
enum是枚举类型, enum用来定义一系列宏定义常量区别用,相当于一系列的#define xx xx,当然它后面的标识符也可当作一个类型标识符。
如:
enum AlertTableSections
{
kUIAction_Simple_Section = 1,
kUIAction_OKCancel_Section,
kUIAction_Custom_Section,
kUIAlert_Simple_Section,
kUIAlert_OKCancel_Section,
kUIAlert_Custom_Section,
};
kUIAction_OKCancel_Section的值为2.
第三、typedef enum 的使用
typedef enum则是用来定义一个数据类型,那么该类型的变量值只能在enum定义的范围内取。
typedef enum {
UIButtonTypeCustom = 0, // no button type
UIButtonTypeRoundedRect, // rounded rect, flat white button, like in address card
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
} UIButtonType;
UIButtonType表示一个类别,它的值只能是UIButtonTypeCustom....
在了解enum和typedef enum的区别之前先应该明白typedef的用法和意义。
C语言里typedef的解释是用来声明新的类型名来代替已有的类姓名,例如:
typedef int CHANGE;
指定了用CHANGE代表int类型,CHANGE代表int,那么:
int a,b;和CHANGE a,b;是等价的、一样的。
方便了个人习惯,熟悉的人用CHANGE来定义int。
typedef为C语言的关键字,作用是为一种数据类型定义一个新名字。这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等)。
而enum是枚举类型,有了typedef的理解容易看出,typedef enum定义了枚举类型,类型变量取值在enum{}范围内取,在使用中二者无差别。
enum AlertTableSections
{
kUIAction_Simple_Section = 0,
kUIAction_OKCancel_Section,
kUIAction_Custom_Section,
kUIAlert_Simple_Section,
kUIAlert_OKCancel_Section,
kUIAlert_Custom_Section,
};
typedef enum {
UIButtonTypeCustom = 0, // no button type
UIButtonTypeRoundedRect, // rounded rect, flat white button, like in address card
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
} UIButtonType;
看上面两个例子更好理解,下面的是UIButton的API,UIButtonType指定的按钮的类型,清楚名了,上面的直接调用enum里的元素就可以了。
相关文章推荐
- IOS开发之----enum与typedef enum的用法
- IOS开发之----enum与typedef enum的用法
- IOS开发之----enum与typedef enum的用法
- iOS开发之 typedef enum 和enum的用法
- 李洪强iOS开发之FMDB线程安全的用法
- 李洪强iOS开发之-FMDB的用法
- 【iOS开发】---- 瀑布流视图控件"quilt"的用法
- iOS开发中随机数的用法
- iOS开发 对话框UIActionSheet和UIAlertView用法
- ios开发self的用法
- IOS开发 NSTimer的用法
- iOS开发视频教程—6.2对象所有权的基本概念和用法
- ios硬件开发 照相机-图像选取器(UIImagePickerController)的用法
- IOS开发Objective-C基础之—self.用法
- IOS开发(31)之关于self.用法的一些总结(转载)
- iOS开发之常见的语句@synthesize obj=_obj的意义详解(self.的用法)
- IOS开发 tableview中cell的用法
- json的用法 SBJson的用法 IOS开发 iphone开发
- IOS开发self.的用法总结
- IOS开发 tableview中cell的用法