iOS 開發(fā)常用宏

引子:

今天一個(gè)前輩解決了我糾結(jié)了很久的問題,具體點(diǎn)說就是怎么在cell里面獲取父視圖的navigationController,籠統(tǒng)點(diǎn)說就是怎么快速在某個(gè)響應(yīng)者里找到一個(gè)類的實(shí)例。

前輩說:“簡(jiǎn)單!”,然后隨手扔了一個(gè)宏給我。

測(cè)試了一下,果然好使,在此處留一個(gè)備份,另塞一些常用的宏,開袋即食,以備不時(shí)之需 。

總覺得自己是在寫作文一樣……


特殊功能

1 . 快速在響應(yīng)者中找到一個(gè)類的實(shí)例。


//快速在響應(yīng)者中找到一個(gè)類的實(shí)例
#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()
圖片版.jpg
使用實(shí)例-獲取superViewController .jpg

通用

1 . 設(shè)備適配相關(guān) 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 . 多線程相關(guān) GCD

// 異步-全局隊(duì)列
#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 . 數(shù)據(jù)存儲(chǔ)相關(guān)

#define LJUserDefaults [NSUserDefaults standardUserDefaults]
#define LJCacheDir     [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
#define LJDocumentDir  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
#define LJTempDir      NSTemporaryDirectory()

6 . 獲取設(shè)備硬件信息

/** 獲取硬件信息*/
#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]

// 支持橫屏的屏幕寬高擴(kuò)展
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 // 當(dāng)前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
// 進(jìn)階寫法 - 可以打印出所在位置和內(nèi)存地址,不如某插件好用,但能用
#ifdef DEBUG
#define LJLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define LJLog(...)
#endif

8 . 線程鎖相關(guān)

// 正常線程鎖的寫法 
static dispatch_once_t onceToken; 
   dispatch_once(&onceToken, ^{ 
   //code 
   });

dispatch_once_t必須是static的,否則報(bào)錯(cuò)。
dispatch_once的第一個(gè)參數(shù)必須取onceToken的地址,就是要“&onceToken。”

這個(gè)時(shí)候可以使用宏來解決

那么這時(shí)候可以用宏定義
#define DISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);
// 簡(jiǎn)單高效……
  DISPATCH_ONCE_BLOCK(^{ 
  //code 
  })

9 . 多線程

//在Main線程上運(yùn)行
#define DISPATCH_ON_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);
//在Global Queue上運(yùn)行
#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(^{ 
   // 異步耗時(shí)任務(wù)
})

10 . 由角度轉(zhuǎn)換弧度 由弧度轉(zhuǎn)換角度

// 有些時(shí)候比M_PI看的更加直觀一點(diǎn) 
#define LJDegreesToRadian(x) (M_PI * (x) / 180.0)
#define LJRadianToDegrees(radian) (radian*180.0)/(M_PI)

11 . 模擬器 vs 真機(jī)

#if TARGET_OS_IPHONE
 //iPhone Device
 #endif

 #if TARGET_IPHONE_SIMULATOR 
//iPhone Simulator 
#endif

一些沒什么卵用但是感覺上似乎是有些卵用的……

自定義block回調(diào)

#define BLOCK_EXE(block, ...) \
if (block) { \
    block(__VA_ARGS__); \
} 

本質(zhì)上跟if (xxx) yyyy; 句式一樣,一個(gè)縮寫而已,但是好過 
!self.clickedBlock ? : self.clickedBlock(); 這樣的語法糖
// 以后少些這種句式 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容