1、多用类型常量,少用#define预处理命令
//ECOAnimatedView.h
extern const CGFloat EOCAnimatedViewAnimationDuration;
//ECOAnimatedView.m
cost CGFloat EOCAnimatedViewAnimationDuration = 0.3f;
不要用预处理命令定义常量。这样定义出来的常量不包含类型信息。
2、用枚举表示状态,选项,状态码
typedef enum {
EOCConnectionStateDisconnected,
EOCConnectionStateConnectiong,
EOCConnectionStateConnected,
EOCConnectionStateDisconnected,
EOCConnectionStateConnectiong,
EOCConnectionStateConnected,
}EOCCOnnectionState;
// 新式语法 宏枚举
typedef NS_ENUM(NSUInteger, EOCConnectionState) {
EOCConnectionStateDisconnected,
EOCConnectionStateConnectiong,
EOCConnectionStateConnected,
typedef NS_ENUM(NSUInteger, EOCConnectionState) {
EOCConnectionStateDisconnected,
EOCConnectionStateConnectiong,
EOCConnectionStateConnected,
};
// 包含选项的枚举类型
typedef NS_OPTIONS(NSUInteger, EOCPermittedDirection) {
EOCPermittedDirectionUp = 1 << 0, // 0001
EOCPermittedDirectionDown = 1 << 1, // 0010
EOCPermittedDirectionLeft = 1 << 2, // 0100
EOCPermittedDirectionRight = 1 << 3, // 1000
typedef NS_OPTIONS(NSUInteger, EOCPermittedDirection) {
EOCPermittedDirectionUp = 1 << 0, // 0001
EOCPermittedDirectionDown = 1 << 1, // 0010
EOCPermittedDirectionLeft = 1 << 2, // 0100
EOCPermittedDirectionRight = 1 << 3, // 1000
};