#import
@interface ViewController : UIViewController
//定义枚举类型
typedef enum {
ENUM_ViewController_ActionTypeStart=0,//开始
ENUM_ViewController_ActionTypeStop,//停止
ENUM_ViewController_ActionTypePause//暂停
} ENUM_ViewController_ActionType;
//-------in parameters---------------
@property (nonatomic,assign) NSInteger InActionType; //操作类型
@end
上面我们就在ViewController.h定义了一个枚举类型, 枚举类型的值默认是连续的自然数 ,例如例子中的
<<带符号左移 (n<<2 将整型值带符号左移2位 )
>>带符号右移 (n>>2 将整型值带符号右移2位 )
>>>无符号右移 (n>>>2 将整型值无符号右移2位 )
<<(左移)
将一个运算符对象的各二进制位全部左移若干位(左边的二进制位丢弃,右边补0)
操作数每左移一位,相当于该数乘以2
例如:3<<2 后,结果为12
此运算符的意思就是把3的二进制位全部左移两位,右边补2个0。3的二进制位11,左移两位后,右边补2个0就是1100。1100转为10进制为12。
说到底左移操作就相当于2的2次方×3。 每左移1位次方就增1
>>(右移)
将一个数的各二进制位全部右移若干位,正数左补0,负数左补1,右边丢弃。
操作数每右移一位,相当于该数除以2
例如:9>>1 后,结果为4
9的二进制为1001,右移1位后,左正数补0,右边丢弃。结果为 0100。转为10进制后为4。
根据位移的常识,我们得出了偏移后的数值
typedef NS_OPTIONS(NSUInteger, ActionType) {
ActionTypeUp = 1 << 0, // 1
ActionTypeDown = 1 << 1, // 2
ActionTypeRight = 1 << 2, // 4
ActionTypeLeft = 1 << 3, // 8
};
一,两者的用法
枚举类型定义用关键字enum标识,形式为:
enum 标识符
{
枚举数据表
};
枚举数据(枚举常量)是一些特定的标识符,标识符代表什么含义,完全由程序员决定。数据枚举的顺序规定了枚举数据的序号,从0开始,依次递增。
enum status
{
copy,
delete
};
枚举类型status仅有两个数据,一个是copy,一个是delete,序号为0、1,代表复制与删除。
enum status
{
copy=6,
delete
};
则copy的序号为6,delete的序号为7。
enum用来定义一系列宏定义常量区别用,相当于一系列的#define xx xx,当然它后面的标识符也可当作一个类型标识符;typedef enum则是用来定义一个数据类型,那么该类型的变量值只能在enum定义的范围内取。两者在这点上是没有差别的。
二,一些说明
用enum定义的类型,只能在它的值域范围内取。比如:
#include "stdafx.h"
enum led_type
{
led_off = 0,
led_half = 127,
led_on = 255,
};
void crake(led_type xx)
{
printf("%d /n",xx);
}
int _tmain()
{
crake(0);
crake(22);
crake(333);
return 0;
}
编译时程序都会报错:error C2664: 'crake' : cannot convert parameter 1 from 'int' to 'led_type'。尽管enum的类型值仍然是整形,但并不意味着它可以接受范围外的整型数。(关于此问题,在Linux中,由别人验证是可以编译通过的。因为两者的C编译法则有差别)
enum和enum typedef 在IOS中的使用
第一、typedef的使用
C语言里typedef的解释是用来声明新的类型名来代替已有的类型名,typedef为C语言的关键字,作用是为一种数据类型定义一个新名字。这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等)
如:typedef char gender;
gender a;与char a;语句相同。
第二 、enum的使用
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;