引子:
今天一个前辈解决了我纠结了很久的问题,具体点说就是怎么在cell里面获取父视图的navigationController,笼统点说就是怎么快速在某个响应者里找到一个类的实例。
前辈说:“简单!”,然后随手扔了一个宏给我。
测试了一下,果然好使,在此处留一个备份,另塞一些常用的宏,开袋即食,以备不时之需 。
总觉得自己是在写作文一样……
特殊功能
1 . 快速在响应者中找到一个类的实例。
//快速在响应者中找到一个类的实例
#define tFindClass(BlockName ,ClassName) ClassName *(^BlockName)() = ^{ \
id objectName = [self nextResponder]; \
while (![objectName isKindOfClass:[ClassName class]] && objectName != nil) { \
objectName = [objectName nextResponder]; \
} \
return objectName; \
}
#define FindClassExample(ExampleName,ClassName) \
tFindClass(myBlock ,ClassName); \
ClassName *ExampleName = myBlock()
通用
1 . 设备适配相关 UIDevice
#define LJiOS_5_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)
#define LJiOS_6_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
#define LJiOS_7_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
#define LJiOS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
#define LJiOS_9_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0)
#define LJiOS_10_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0)
2 . 多线程相关 GCD
// 异步-全局队列
#define LJGlobalGCD(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
// 主线程
#define LJMainGCD(block) dispatch_async(dispatch_get_main_queue(),block)
3 . 颜色 UIColor
// RGB
#define LJRGBColor(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
// RGB + Alpha
#define LJRGBAColor(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
// 16位真彩色
#define LJRGB16Color(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]
4 . 加载本地文件
// 加载本地图片
#define LJLoadImage(file,type) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:type]]
#define LJLoadArray(file,type) [UIImage arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:type]]
#define LJLoadDict(file,type) [UIImage dictionaryWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:type]]
5 . 数据存储相关
#define LJUserDefaults [NSUserDefaults standardUserDefaults]
#define LJCacheDir [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
#define LJDocumentDir [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
#define LJTempDir NSTemporaryDirectory()
6 . 获取设备硬件信息
/** 获取硬件信息*/
#define LJSCREEN_W [UIScreen mainScreen].bounds.size.width
#define LJSCREEN_H [UIScreen mainScreen].bounds.size.height
#define LJCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
#define LJCurrentSystemVersion [[[UIDevice currentDevice] systemVersion] floatValue]
// 支持横屏的屏幕宽高扩展
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 // 当前Xcode支持iOS8及以上
#define SCREEN_WIDTH ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.width)
#define SCREENH_HEIGHT ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.height)
#define SCREEN_SIZE ([[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)
#else
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREEN_SIZE [UIScreen mainScreen].bounds.size
#endif
7 . Log
// 不管怎么说……你的确是宏啊
#ifdef DEBUG
#define LJLog(...) NSLog(__VA_ARGS__)
#else
#define LJLog(...)
#endif
// 进阶写法 - 可以打印出所在位置和内存地址,不如某插件好用,但能用
#ifdef DEBUG
#define LJLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define LJLog(...)
#endif
8 . 线程锁相关
// 正常线程锁的写法
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//code
});
dispatch_once_t必须是static的,否则报错。
dispatch_once的第一个参数必须取onceToken的地址,就是要“&onceToken。”
这个时候可以使用宏来解决
那么这时候可以用宏定义
#define DISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);
// 简单高效……
DISPATCH_ONCE_BLOCK(^{
//code
})
9 . 多线程
//在Main线程上运行
#define DISPATCH_ON_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);
//在Global Queue上运行
#define DISPATCH_ON_GLOBAL_QUEUE_HIGH(globalQueueBlocl) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), globalQueueBlocl);
#define DISPATCH_ON_GLOBAL_QUEUE_DEFAULT(globalQueueBlocl) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);
#define DISPATCH_ON_GLOBAL_QUEUE_LOW(globalQueueBlocl) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), globalQueueBlocl);
#define DISPATCH_ON_GLOBAL_QUEUE_BACKGROUND(globalQueueBlocl) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), globalQueueBlocl);
// 主线程上
DISPATCH_ON_MAIN_THREAD(^{
// 更新UI
})
// Global Queue
// 用的最多的还是default模式
DISPATCH_ON_GLOBAL_QUEUE_DEFAULT(^{
// 异步耗时任务
})
10 . 由角度转换弧度 由弧度转换角度
// 有些时候比M_PI看的更加直观一点
#define LJDegreesToRadian(x) (M_PI * (x) / 180.0)
#define LJRadianToDegrees(radian) (radian*180.0)/(M_PI)
11 . 模拟器 vs 真机
#if TARGET_OS_IPHONE
//iPhone Device
#endif
#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif
一些没什么卵用但是感觉上似乎是有些卵用的……
自定义block回调
#define BLOCK_EXE(block, ...) \
if (block) { \
block(__VA_ARGS__); \
}
本质上跟if (xxx) yyyy; 句式一样,一个缩写而已,但是好过
!self.clickedBlock ? : self.clickedBlock(); 这样的语法糖
// 以后少些这种句式