- 多用const常量类型少用#define预处理指令
- 枚举来表示状态,选项,状态码
typedef NS_ENUM(NSUInteger,NETConnectionState) {
NETConnectionStateDisconnected,
NETConnectionStateConnecting = 3,
NETConnectionStateConnected
};
NETConnectionState netState = NETConnectionStateConnecting;
NSLog(@"netState : %lu",netState); // netState : 3
NSLog(@"NETConnectionStateDisconnected : %lu",(unsigned long)NETConnectionStateDisconnected); // NETConnectionStateDisconnected : 0
NSLog(@"NETConnectionStateConnecting : %lu",(unsigned long)NETConnectionStateConnecting); // NETConnectionStateConnecting : 3
NSLog(@"NETConnectionStateConnected : %lu",(unsigned long)NETConnectionStateConnected); // NETConnectionStateConnected : 4
可以将一些公共常量和枚举放在一个.h文件中
- Const.h 存放一些公共常量枚举文件
NSString * const terminal = @"iPhone";
// 职业
typedef NS_ENUM(NSUInteger,EmployeeType) {
EmployeeTypeDeveloper,
EmployeeTypeDesigner,
EmployeeTypeFinance,
};
// 网络状态
typedef NS_ENUM(NSUInteger,NETConnectionState) {
NETConnectionStateDisconnected,
NETConnectionStateConnecting = 3,
NETConnectionStateConnected
};
- 实现文件
#import <Foundation/Foundation.h>
#import "Const.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NETConnectionState netState = NETConnectionStateConnecting;
NSLog(@"netState : %lu",netState); // netState : 3
NSLog(@"NETConnectionStateDisconnected : %lu",(unsigned long)NETConnectionStateDisconnected); // NETConnectionStateDisconnected : 0
NSLog(@"NETConnectionStateConnecting : %lu",(unsigned long)NETConnectionStateConnecting); // NETConnectionStateConnecting : 3
NSLog(@"NETConnectionStateConnected : %lu",(unsigned long)NETConnectionStateConnected); // NETConnectionStateConnected : 4
NSLog(@"终端: %@",terminal);
}
return 0;
}
网友评论