1.NSLog使用
调试程序的时候,会用到NSLog打印一些日志,然后上线的时候都注释掉,可以定义一个宏来实现
#ifdef DEBUG
#define DLog(format, ...) NSLog((@"%s" "[Line:%d]" format),__FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define DLog(...);
#endif
// __FILE__ 文件名地址
// __FUNCTION__ 函数名
// __LINE__ 行数
// ##__VA_ARGS__ 会清除掉多余的逗号
Xcode8打印JSON的时候,NSLog控制台显示不完整,可以通过以下宏代替
#ifdef DEBUG
#define DLog(...) printf("%s %s [Line:%d] %s",__TIME__,__FUNCTION__,__LINE__,[[NSString stringWithFormat:__VA_ARGS__] UTF8String]);
#else
#define DLog(...);
#endif
2.获取系统版本号
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
3.获取当前语言
#define CURRENT_LANGUAGE ([[NSLocale preferredLanguages] objectAtIndex:0])
4.屏幕高度和宽度
#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
5.判断设备
#define IS_IHONE5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define IS_IHONE6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
#define IS_IHONE6_Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
#define IS_IPHONE [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone
#define IS_IPAD [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad
#define ISIPHONEX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
6.判断真机和模拟器
#if TARGET_OS_IPHONE
// iPhone Device
#endif
#if TARGET_IPHONE_SIMULATOR
// iPhone Simulator
#endif
7.检查系统版本
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
7.内存相关
// 使用ARC和不使用ARC
#if __has_feature(objc_arc)
// compiling with ARC
#else
// compiling without ARC
#endif
#define RELEASE_SAFELY(__POINTER) { if(__POINTER) {[__POINTER release]; __POINTER = nil; }}
#define AUTORELEASE_SAFELY(__POINTER) { [__POINTER autorelease]; __POINTER = nil; }
8.弧度和角度转化
#define DEGREES_TO_RADIAN(x) (M_PI * (x) / 180.0)
#define RADIAN_TO_DEGREES(radian) (radian * 180.0) / (M_PI)