enum类型通常会与typedef关键字一起用,方便使用.
有两种写法,
第一种是typedef在enum前边,如:
typedef enum {
UIViewAnimationCurveEaseInOut, // slow at beginning and end
UIViewAnimationCurveEaseIn, // slow at beginning
UIViewAnimationCurveEaseOut, // slow at end
UIViewAnimationCurveLinear
} UIViewAnimationCurve;
enum {
DDLogMessageCopyFile = 1 << 0,
DDLogMessageCopyFunction = 1 << 1,
};
typedef int DDLogMessageOptions;
两者有何区别呢?先看DDLogMessageOptions定义体, <<是左移运算,定义这样一些值通常是为了使这些值除了离散的options之外还有"|"运算的目的,从而组成更多的options,也就是options不仅限定义体中的那几个.
这时如果用如下定义,
typedef enum {
DDLogMessageCopyFile = 1 << 0,
DDLogMessageCopyFunction = 1 << 1,
} DDLogMessageOptions;
那么
DDLogMessageOptions op = DDLogMessageCopyFile | DDLogMessageCopyFunction 将编译不过(VS会报错,Xcode却不报语法错误,看来Xcode标准遵循得不是很好), 也许有人说强制转型, 但不转型显然更合逻辑与简练.