【Objective-C】常用的宏定义
2020-11-26 本文已影响0人
MR_詹
/// App版本号
#define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
/// App内建版本号
#define kAppBuildVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]
/// 系统版本号
#define kSystemVersion [[UIDevice currentDevice] systemVersion]
/// 不同系统版本号
#define IOS8 ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0 && [[UIDevice currentDevice].systemVersion doubleValue] < 9.0)
#define IOS9 ([[UIDevice currentDevice].systemVersion doubleValue] >= 9.0 && [[UIDevice currentDevice].systemVersion doubleValue] < 10.0)
#define IOS10 ([[UIDevice currentDevice].systemVersion doubleValue] >= 10.0 && [[UIDevice currentDevice].systemVersion doubleValue] < 11.0)
#define IOS11 ([[UIDevice currentDevice].systemVersion doubleValue] >= 11.0 && [[UIDevice currentDevice].systemVersion doubleValue] < 12.0)
#define IOS12 ([[UIDevice currentDevice].systemVersion doubleValue] >= 12.0 && [[UIDevice currentDevice].systemVersion doubleValue] < 13.0)
#define IOS13 ([[UIDevice currentDevice].systemVersion doubleValue] >= 13.0 && [[UIDevice currentDevice].systemVersion doubleValue] < 14.0)
#define IOS14 ([[UIDevice currentDevice].systemVersion doubleValue] >= 14.0 && [[UIDevice currentDevice].systemVersion doubleValue] < 15.0)
#define IOS8_LATER @available(iOS 8.0, *)
#define IOS9_LATER @available(iOS 9.0, *)
#define IOS10_LATER @available(iOS 10.0, *)
#define IOS11_LATER @available(iOS 11.0, *)
#define IOS12_LATER @available(iOS 12.0, *)
#define IOS13_LATER @available(iOS 13.0, *)
#define IOS14_LATER @available(iOS 14.0, *)
/// Document文件路径
#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
/// Library文件路径
#define kLibraryPath [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject]
/// Cache文件路径
#define kCachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
/// Temporary文件路径
#define kTempPath NSTemporaryDirectory()
/// Application
#define kApplication [UIApplication sharedApplication]
/// Appdelegate
#define kAppDelegate [UIApplication sharedApplication].delegate
/// UserDefaults
#define kUserDefaults [NSUserDefaults standardUserDefaults]
/// NotificationCenter
#define kNotificationCenter [NSNotificationCenter defaultCenter]
/// 判断字符串是否为空
#define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
/// 判断数组是否为空
#define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
/// 判断字典是否为空
#define kDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)
/// 判断是否为空对象
#define kObjectIsEmpty(_object) (_object == nil \
|| [_object isKindOfClass:[NSNull class]] \
|| ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \
|| ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))
/// 屏幕宽度
#define kScreenW \
([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? [UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.width)
/// 屏幕高度
#define kScreenH \
([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? [UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.height)
/// 屏幕尺寸
#define kScreenS \
([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? CGSizeMake([UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale,[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale) : [UIScreen mainScreen].bounds.size)
/// 屏幕适配(以屏幕宽度为375的设计图为准)
#define Scale(x) ((kScreenW>=375)?x:kScreenW*(x)/375.0)
/// GCD - 一次性执行
#define DISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);
/// GCD - 在Main线程上运行
#define DISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);
/// GCD - 开启异步线程
#define DISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);
/// GCD - 定时器
#define DISPATCH_AFTER_BLOCK(duartion,onceBlock) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duartion * NSEC_PER_SEC)), dispatch_get_main_queue(),onceBlock);
/// 拼接字符串
#define NSStringFormat(format,...) [NSString stringWithFormat:format,##__VA_ARGS__]
/// 加载本地图片
#define ImageNamed(name) [UIImage imageNamed:[UIUtil imageName:name]]
/// RGB颜色
#define RGB(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
/// RGBA颜色
#define RGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a]
/// 随机颜色
#define RandomColor KRGBColor(arc4random_uniform(256)/255.0,arc4random_uniform(256)/255.0,arc4random_uniform(256)/255.0)
/// HEX颜色
#define HexColor (rgbValue) \
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16)) / 255.0 \
green:((float)((rgbValue & 0xFF00) >> 8)) / 255.0 \
blue:((float)(rgbValue & 0xFF)) / 255.0 alpha:1.0]
/// 系统默认字体
#define SystemFont(x) [UIFont systemFontOfSize:x]
/// 系统粗体
#define BoldFont(x) [UIFont boldSystemFontOfSize:x]
/// 系统中体
#define MediumFont(x) [UIFont fontWithName:@"PingFangSC-Medium" size:x]
/// 角度 转 弧度
#define DegreesToRadian(x) (M_PI * (x) / 180.0)
/// 弧度 转 角度
#define RadianToDegrees(radian) (radian * 180.0) / (M_PI)
/// 时间间隔:开始时间
#define StartTime CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
/// 时间间隔:结束时间
#define EndTime NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)
/// 控制台打印
#ifdef DEBUG
#define NSLog(...) NSLog(@"%s 第%d行 : %@\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define NSLog(...)
#endif