iOS 常用宏定义

来源:互联网 发布:四三九九网络 编辑:程序博客网 时间:2024/06/05 06:36

Tip:多用常量类型,少用#define预处理指令
#define DURATION 3
预处理过程会把所有碰到的DURATION替换成3,有可能和常量名冲突

static const NSTimeInterval kDuration = ;用此方式定义的常量 不能被修改,如果视图修改由const修饰符声明的变量,编译器会报错。

1.定义屏幕宽高宏定义

#define kScreenW [UIScreen mainScreen].bounds.size.width#define kScreenH [UIScreen mainScreen].bounds.size.height

2.定义NSLog输出位置宏定义

#ifdef DEBUG#define NSLog(FORMAT, ...) fprintf(stderr,"%s:%d\t%s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);#else#define NSLog(FORMAT, ...) nil#endif

3.WeakSelf宏

#define WeakSelf __weak typeof(self) weakSelf = self;

使用时需要声明,例如

 WeakSelf; weakSelf doSomething...

4.当前系统版本宏

#define kSystemVersion [[UIDevice currentDevice].systemVersion floatValue]
0 0