什么是預(yù)編譯
從字面上來理解就是提前編譯的意思绍移,也就是說在編譯器在開始真正的編譯前進行的編譯倦微,在iOS開發(fā)中也就是我們按下command+d之前的編譯就是預(yù)編譯揍瑟。預(yù)編譯的工作是Xcode幫我們做的少欺。其實喳瓣,你也可以稱預(yù)編譯為預(yù)處理,說到頭就是Xcode在開始編譯前做的一些自動操作赞别。
預(yù)編譯頭畏陕,預(yù)編譯頭文件
預(yù)編譯頭(precompiled header)是程序設(shè)計時把頭文件編譯為中間格式(如目標文件),以節(jié)約在開發(fā)過程中編譯器反復(fù)編譯該頭文件的開銷仿滔。 --[維基百科]
為減少編譯時間惠毁,編譯器允許把頭文件編譯為某種中間形式稱為預(yù)編譯頭,后續(xù)再編譯源文件時就可以盡量直接使用這些預(yù)編譯頭崎页。在iOS中的那個.pch
結(jié)尾的文件就是預(yù)編譯頭文件鞠绰。就是在我們開始編譯前,在預(yù)編譯頭文件里的文件Xcode都會提前給我們編譯好飒焦,如果這個文件包含的頭文件沒有改動蜈膨,那么編譯器就不會去編譯這些這些文件。并且在編譯階段牺荠,預(yù)編譯頭文件的內(nèi)容會被默認替換到每一個源文件的開頭翁巍。也就是說你放在預(yù)編譯頭文件里的.h
文件你可以全局使用而不用在導(dǎo)入一遍。
Modules:解決預(yù)編譯頭文件帶來的弊端問題
預(yù)編譯的分類
- 宏定義
- 文件包含
- 條件編譯
宏定義
宏定義又叫宏替換志电,所以從字面上來理解宏定義是用來做替換的曙咽。自然也就沒有計算或者表達式求解之類的功能蛔趴,也不會分配內(nèi)存空間挑辆。
宏定義在開發(fā)中一般用于一個文件或者整個工程很多地方都用到的數(shù)字例朱,字符串等。其實宏定義就是把很多地方都用到的量放在一個地方管理鱼蝉,類似我們的抽象封裝思想洒嗤。宏定義的定義如下:
#define 標識符 字符串
比如我們工程中很多地方用到圓周率,我們就可以一個宏來表示圓周率眉抬。
#define PI 3.14
這樣患民,只要我們工廠中使用PI的地方都表示3.14怠肋。為了和一半的常量和變量區(qū)別,宏定義我們一般用大寫的字母表示间唉。
含參數(shù)的宏定義
宏定義也可以有參數(shù),比如我們定義一個做簡單加法的宏定義
#define ADD_INT(a,b) a+b
使用有參數(shù)的宏定義有些像函數(shù)
NSLog("1+2=%i",ADD_INT(1,2));//打印結(jié)果為:1+2=3
#undef
如果想終止宏定義的作用利术,可以用關(guān)鍵字#undef
來終止某一個宏的作用呈野,拿上面的宏PI
來舉例。
#undef PI
//該語句下面的PI就不表示3.14了
也許你會說這個關(guān)鍵字的應(yīng)用場景沒有啊印叁,你別說我還真遇到有一個被冒,看下面的代碼:
//#undef DEBUG //打開注釋使用DEBUG狀態(tài)線上環(huán)境
#ifdef DEBUG
//線下環(huán)境
#else
//線上環(huán)境
#endif
如果我們想再DEBUG
狀態(tài)下使用線上環(huán)境我們就可以在這段代碼之前使用#undef
來很方便的達到目的。
宏定義中的換行
如果宏定義的字符串
部分有很多行轮蜕,需要換行昨悼,這時候需要在每一行的后面加反斜杠\
,如下面的這個宏
#define SINGLEIMPLEMENTATION(className) static className *instance_; \
\
+ (instancetype)share##className \
{ \
if (!instance_) { \
\
instance_ = [[self alloc] init]; \
} \
\
return instance_; \
}
需要注意的是最后一行不用反斜杠和空行也要用反斜杠跃洛。
文件包含
顧名思義就是用來講一個文件包含到另一個文件中的宏率触。
在C語言中,我們使用的#include
和在OC中使用的#import
都是文件包含指令税课。他的作用是包含一個源文件代碼闲延。在OC中還有一個文件包含的指令@class
,他的作用是聲明一個類,告訴編譯器它后面的名字是一個類的名字,而這個類的定義實現(xiàn)是暫時不用知道的韩玩。這里需要注意的是#include
和#import
的區(qū)別垒玲。
#include //有可能引起重復(fù)引用問題
#import //不會引起重復(fù)引用問題
條件編譯
條件編譯就是在編譯之前預(yù)處理器根據(jù)預(yù)處理指令判斷對應(yīng)的條件,如果條件滿足就將對應(yīng)的代碼編譯進去找颓,否則代碼就根本不進入編譯環(huán)節(jié)(相當于根本就沒有這段代碼)合愈。
先看一下條件編譯有哪些吧
1.#if 編譯預(yù)處理中的條件命令, 相當于C語法中的if語句
2.#ifdef 判斷某個宏是否被定義, 若已定義, 執(zhí)行隨后的語句
3.#ifndef 與#ifdef相反, 判斷某個宏是否未被定義
4.#elif 若#if, #ifdef, #ifndef或前面的#elif條件不滿足, 則執(zhí)行#elif之后的語句, 相當于C語法中的else-if
5.#else 與#if, #ifdef, #ifndef對應(yīng), 若這些條件不滿足, 則執(zhí)行#else之后的語句, 相當于C語法中的else
6.#endif #if, #ifdef, #ifndef這些條件命令的結(jié)束標志.
7.#if 與 #ifdef 的區(qū)別:#if是判斷后面的條件語句是否成立,#ifdef是判斷某個宏是否被定義過击狮。要區(qū)分開佛析!
使用場景剛才在上文中有用到。
錯誤彪蓬,警告的預(yù)處理
#error
當程序檢查到這里時會停止編譯寸莫,這個命令的作用是在錯誤的地方禁止編譯。
#warning
這個命令并不會影響程序的編譯和運行档冬,但是會認為的在這里顯示一條警告信息膘茎,提醒我們自己桃纯。
編譯器控制指令
就是給編譯器直接下的指令
#pragma 參數(shù)
這個預(yù)編譯指令是最復(fù)雜的,用于控制編譯器的行為披坏,一般我們開發(fā)應(yīng)用APP是很少用到的态坦,常用的有兩種方式:
#pragma mark - 信息
為代碼加上標注
#pragma message("信息")
編譯時提示信息
常見的宏定義
以下來自網(wǎng)絡(luò)搜集
#define APPDELEGATE [(AppDelegate*)[UIApplication sharedApplication] delegate]
//----------------------系統(tǒng)設(shè)備相關(guān)----------------------------
//獲取設(shè)備屏幕尺寸
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)//應(yīng)用尺寸
#define APP_WIDTH [[UIScreen mainScreen]applicationFrame].size.width
#define APP_HEIGHT [[UIScreen mainScreen]applicationFrame].size.height
//獲取系統(tǒng)版本
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
#define CurrentSystemVersion [[UIDevice currentDevice] systemVersion]
#define isIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]==4)
#define isIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]==5)
#define isIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]==6)
#define isAfterIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]>4)
#define isAfterIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]>5)
#define isAfterIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]>6)
//獲取當前語言
#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
//判斷是否 Phone 4/5/6 是否是iPad
#define Phone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
#define Phone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define Phone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
#define Phone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242,2208), [[UIScreen mainScreen] currentMode].size) : NO)
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
//判斷是真機還是模擬器
#if TARGET_OS_IPHONE
//iPhone Device
#endif
#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif
//----------------------系統(tǒng)設(shè)備相關(guān)----------------------------
//----------------------內(nèi)存相關(guān)----------------------------
//使用ARC和不使用ARC
#if __has_feature(objc_arc)
//compiling with ARC
#else
// compiling without ARC
#endif
//釋放一個對象
#define SAFE_DELETE(P) if(P) { [P release], P = nil; }
#define SAFE_RELEASE(x) [x release];x=nil
//----------------------內(nèi)存相關(guān)----------------------------
//----------------------圖片相關(guān)----------------------------
//讀取本地圖片
#define LOADIMAGE(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]
//定義UIImage對象
#define IMAGE(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]]
//定義UIImage對象
#define ImageNamed(_pointer) [UIImage imageNamed:_pointer]
//可拉伸的圖片
#define ResizableImage(name,top,left,bottom,right) [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right)]
#define ResizableImageWithMode(name,top,left,bottom,right,mode) [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right) resizingMode:mode]
//建議使用前兩種宏定義,性能高于后者
//----------------------圖片相關(guān)----------------------------
//----------------------顏色相關(guān)---------------------------
// rgb顏色轉(zhuǎn)換(16進制->10進制)
#define UIColorFromRGB(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]
// 獲取RGB顏色
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define RGB(r,g,b) RGBA(r,g,b,1.0f)
//背景色
#define BACKGROUND_COLOR [UIColor colorWithRed:242.0/255.0 green:236.0/255.0 blue:231.0/255.0 alpha:1.0]
//清除背景色
#define CLEARCOLOR [UIColor clearColor]
//----------------------顏色相關(guān)--------------------------
//----------------------其他----------------------------
//方正黑體簡體字體定義
#define FONT(F) [UIFont fontWithName:@"FZHTJW--GB1-0" size:F]
//file
//讀取文件的文本內(nèi)容,默認編碼為UTF-8
#define FileString(name,ext) [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)] encoding:NSUTF8StringEncoding error:nil]
#define FileDictionary(name,ext) [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)]]
#define FileArray(name,ext) [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)]]
//G-C-D
#define BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
#define MAIN(block) dispatch_async(dispatch_get_main_queue(),block)
//Alert
#define ALERT(msg) [[[UIAlertView alloc] initWithTitle:nil message:msg delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil] show]
//由角度獲取弧度 有弧度獲取角度
#define degreesToRadian(x) (M_PI * (x) / 180.0)
#define radianToDegrees(radian) (radian*180.0)/(M_PI)
//----------------------其他-------------------------------
//----------------------視圖相關(guān)----------------------------
//設(shè)置需要粘貼的文字或圖片
#define PasteString(string) [[UIPasteboard generalPasteboard] setString:string];
#define PasteImage(image) [[UIPasteboard generalPasteboard] setImage:image];
//得到視圖的left top的X,Y坐標點
#define VIEW_TX(view) (view.frame.origin.x)
#define VIEW_TY(view) (view.frame.origin.y)
//得到視圖的right bottom的X,Y坐標點
#define VIEW_BX(view) (view.frame.origin.x + view.frame.size.width)
#define VIEW_BY(view) (view.frame.origin.y + view.frame.size.height )
//得到視圖的尺寸:寬度、高度
#define VIEW_W(view) (view.frame.size.width)
#define VIEW_H(view) (view.frame.size.height)
//得到frame的X,Y坐標點
#define FRAME_TX(frame) (frame.origin.x)
#define FRAME_TY(frame) (frame.origin.y)
//得到frame的寬度棒拂、高度
#define FRAME_W(frame) (frame.size.width)
#define FRAME_H(frame) (frame.size.height)
//----------------------視圖相關(guān)----------------------------
//---------------------打印日志--------------------------
//Debug模式下打印日志,當前行,函數(shù)名
#if DEBUG
#define DLog(FORMAT, ...) fprintf(stderr,"\nfunction:%s line:%d content:%s\n", __FUNCTION__, __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
#define NSLog(FORMAT, ...) nil
#endif
//Debug模式下打印日志,當前行,函數(shù)名 并彈出一個警告
#ifdef DEBUG
# define WDLog(fmt, ...) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%s\n [Line %d] ", __PRETTY_FUNCTION__, __LINE__] message:[NSString stringWithFormat:fmt, ##__VA_ARGS__] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; }
#else
# define NSLog(...)
#endif
//打印Frame
#define LogFrame(frame) NSLog(@"frame[X=%.1f,Y=%.1f,W=%.1f,H=%.1f",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height)
//打印Point
#define LogPoint(point) NSLog(@"Point[X=%.1f,Y=%.1f]",point.x,point.y)
//---------------------打印日志--------------------------
預(yù)編譯指令還有很多玩法伞梯,以后補充了。
什么是預(yù)編譯
從字面上來理解就是提前編譯的意思帚屉,也就是說在編譯器在開始真正的編譯前進行的編譯谜诫,在iOS開發(fā)中也就是我們按下command+d之前的編譯就是預(yù)編譯。預(yù)編譯的工作是Xcode幫我們做的攻旦。其實猜绣,你也可以稱預(yù)編譯為預(yù)處理,說到頭就是Xcode在開始編譯前做的一些自動操作敬特。
預(yù)編譯頭掰邢,預(yù)編譯頭文件
預(yù)編譯頭(precompiled header)是程序設(shè)計時把頭文件編譯為中間格式(如目標文件),以節(jié)約在開發(fā)過程中編譯器反復(fù)編譯該頭文件的開銷伟阔。 --[維基百科]
為減少編譯時間辣之,編譯器允許把頭文件編譯為某種中間形式稱為預(yù)編譯頭,后續(xù)再編譯源文件時就可以盡量直接使用這些預(yù)編譯頭皱炉。在iOS中的那個.pch
結(jié)尾的文件就是預(yù)編譯頭文件怀估。就是在我們開始編譯前,在預(yù)編譯頭文件里的文件Xcode都會提前給我們編譯好合搅,如果這個文件包含的頭文件沒有改動多搀,那么編譯器就不會去編譯這些這些文件。并且在編譯階段灾部,預(yù)編譯頭文件的內(nèi)容會被默認替換到每一個源文件的開頭康铭。也就是說你放在預(yù)編譯頭文件里的.h
文件你可以全局使用而不用在導(dǎo)入一遍。
Modules:解決預(yù)編譯頭文件帶來的弊端問題
預(yù)編譯的分類
- 宏定義
- 文件包含
- 條件編譯
宏定義
宏定義又叫宏替換赌髓,所以從字面上來理解宏定義是用來做替換的从藤。自然也就沒有計算或者表達式求解之類的功能,也不會分配內(nèi)存空間锁蠕。
宏定義在開發(fā)中一般用于一個文件或者整個工程很多地方都用到的數(shù)字夷野,字符串等。其實宏定義就是把很多地方都用到的量放在一個地方管理荣倾,類似我們的抽象封裝思想悯搔。宏定義的定義如下:
#define 標識符 字符串
比如我們工程中很多地方用到圓周率,我們就可以一個宏來表示圓周率舌仍。
#define PI 3.14
這樣妒貌,只要我們工廠中使用PI的地方都表示3.14者娱。為了和一半的常量和變量區(qū)別,宏定義我們一般用大寫的字母表示苏揣。
含參數(shù)的宏定義
宏定義也可以有參數(shù),比如我們定義一個做簡單加法的宏定義
#define ADD_INT(a,b) a+b
使用有參數(shù)的宏定義有些像函數(shù)
NSLog("1+2=%i",ADD_INT(1,2));//打印結(jié)果為:1+2=3
#undef
如果想終止宏定義的作用推姻,可以用關(guān)鍵字#undef
來終止某一個宏的作用平匈,拿上面的宏PI
來舉例。
#undef PI
//該語句下面的PI就不表示3.14了
也許你會說這個關(guān)鍵字的應(yīng)用場景沒有啊藏古,你別說我還真遇到有一個增炭,看下面的代碼:
//#undef DEBUG //打開注釋使用DEBUG狀態(tài)線上環(huán)境
#ifdef DEBUG
//線下環(huán)境
#else
//線上環(huán)境
#endif
如果我們想再DEBUG
狀態(tài)下使用線上環(huán)境我們就可以在這段代碼之前使用#undef
來很方便的達到目的。
宏定義中的換行
如果宏定義的字符串
部分有很多行拧晕,需要換行隙姿,這時候需要在每一行的后面加反斜杠\
,如下面的這個宏
#define SINGLEIMPLEMENTATION(className) static className *instance_; \
\
+ (instancetype)share##className \
{ \
if (!instance_) { \
\
instance_ = [[self alloc] init]; \
} \
\
return instance_; \
}
需要注意的是最后一行不用反斜杠和空行也要用反斜杠厂捞。
文件包含
顧名思義就是用來講一個文件包含到另一個文件中的宏输玷。
在C語言中,我們使用的#include
和在OC中使用的#import
都是文件包含指令靡馁。他的作用是包含一個源文件代碼欲鹏。在OC中還有一個文件包含的指令@class
,他的作用是聲明一個類,告訴編譯器它后面的名字是一個類的名字,而這個類的定義實現(xiàn)是暫時不用知道的臭墨。這里需要注意的是#include
和#import
的區(qū)別赔嚎。
#include //有可能引起重復(fù)引用問題
#import //不會引起重復(fù)引用問題
條件編譯
條件編譯就是在編譯之前預(yù)處理器根據(jù)預(yù)處理指令判斷對應(yīng)的條件,如果條件滿足就將對應(yīng)的代碼編譯進去胧弛,否則代碼就根本不進入編譯環(huán)節(jié)(相當于根本就沒有這段代碼)尤误。
先看一下條件編譯有哪些吧
1.#if 編譯預(yù)處理中的條件命令, 相當于C語法中的if語句
2.#ifdef 判斷某個宏是否被定義, 若已定義, 執(zhí)行隨后的語句
3.#ifndef 與#ifdef相反, 判斷某個宏是否未被定義
4.#elif 若#if, #ifdef, #ifndef或前面的#elif條件不滿足, 則執(zhí)行#elif之后的語句, 相當于C語法中的else-if
5.#else 與#if, #ifdef, #ifndef對應(yīng), 若這些條件不滿足, 則執(zhí)行#else之后的語句, 相當于C語法中的else
6.#endif #if, #ifdef, #ifndef這些條件命令的結(jié)束標志.
7.#if 與 #ifdef 的區(qū)別:#if是判斷后面的條件語句是否成立,#ifdef是判斷某個宏是否被定義過结缚。要區(qū)分開损晤!
使用場景剛才在上文中有用到。
錯誤红竭,警告的預(yù)處理
#error
當程序檢查到這里時會停止編譯沉馆,這個命令的作用是在錯誤的地方禁止編譯。
#warning
這個命令并不會影響程序的編譯和運行德崭,但是會認為的在這里顯示一條警告信息斥黑,提醒我們自己。
編譯器控制指令
就是給編譯器直接下的指令
#pragma 參數(shù)
這個預(yù)編譯指令是最復(fù)雜的眉厨,用于控制編譯器的行為锌奴,一般我們開發(fā)應(yīng)用APP是很少用到的,常用的有兩種方式:
#pragma mark - 信息
為代碼加上標注
#pragma message("信息")
編譯時提示信息
常見的宏定義
以下來自網(wǎng)絡(luò)搜集
#define APPDELEGATE [(AppDelegate*)[UIApplication sharedApplication] delegate]
//----------------------系統(tǒng)設(shè)備相關(guān)----------------------------
//獲取設(shè)備屏幕尺寸
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)//應(yīng)用尺寸
#define APP_WIDTH [[UIScreen mainScreen]applicationFrame].size.width
#define APP_HEIGHT [[UIScreen mainScreen]applicationFrame].size.height
//獲取系統(tǒng)版本
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
#define CurrentSystemVersion [[UIDevice currentDevice] systemVersion]
#define isIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]==4)
#define isIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]==5)
#define isIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]==6)
#define isAfterIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]>4)
#define isAfterIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]>5)
#define isAfterIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]>6)
//獲取當前語言
#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
//判斷是否 Phone 4/5/6 是否是iPad
#define Phone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
#define Phone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define Phone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
#define Phone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242,2208), [[UIScreen mainScreen] currentMode].size) : NO)
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
//判斷是真機還是模擬器
#if TARGET_OS_IPHONE
//iPhone Device
#endif
#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif
//----------------------系統(tǒng)設(shè)備相關(guān)----------------------------
//----------------------內(nèi)存相關(guān)----------------------------
//使用ARC和不使用ARC
#if __has_feature(objc_arc)
//compiling with ARC
#else
// compiling without ARC
#endif
//釋放一個對象
#define SAFE_DELETE(P) if(P) { [P release], P = nil; }
#define SAFE_RELEASE(x) [x release];x=nil
//----------------------內(nèi)存相關(guān)----------------------------
//----------------------圖片相關(guān)----------------------------
//讀取本地圖片
#define LOADIMAGE(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]
//定義UIImage對象
#define IMAGE(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]]
//定義UIImage對象
#define ImageNamed(_pointer) [UIImage imageNamed:_pointer]
//可拉伸的圖片
#define ResizableImage(name,top,left,bottom,right) [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right)]
#define ResizableImageWithMode(name,top,left,bottom,right,mode) [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right) resizingMode:mode]
//建議使用前兩種宏定義,性能高于后者
//----------------------圖片相關(guān)----------------------------
//----------------------顏色相關(guān)---------------------------
// rgb顏色轉(zhuǎn)換(16進制->10進制)
#define UIColorFromRGB(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]
// 獲取RGB顏色
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define RGB(r,g,b) RGBA(r,g,b,1.0f)
//背景色
#define BACKGROUND_COLOR [UIColor colorWithRed:242.0/255.0 green:236.0/255.0 blue:231.0/255.0 alpha:1.0]
//清除背景色
#define CLEARCOLOR [UIColor clearColor]
//----------------------顏色相關(guān)--------------------------
//----------------------其他----------------------------
//方正黑體簡體字體定義
#define FONT(F) [UIFont fontWithName:@"FZHTJW--GB1-0" size:F]
//file
//讀取文件的文本內(nèi)容,默認編碼為UTF-8
#define FileString(name,ext) [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)] encoding:NSUTF8StringEncoding error:nil]
#define FileDictionary(name,ext) [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)]]
#define FileArray(name,ext) [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)]]
//G-C-D
#define BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
#define MAIN(block) dispatch_async(dispatch_get_main_queue(),block)
//Alert
#define ALERT(msg) [[[UIAlertView alloc] initWithTitle:nil message:msg delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil] show]
//由角度獲取弧度 有弧度獲取角度
#define degreesToRadian(x) (M_PI * (x) / 180.0)
#define radianToDegrees(radian) (radian*180.0)/(M_PI)
//----------------------其他-------------------------------
//----------------------視圖相關(guān)----------------------------
//設(shè)置需要粘貼的文字或圖片
#define PasteString(string) [[UIPasteboard generalPasteboard] setString:string];
#define PasteImage(image) [[UIPasteboard generalPasteboard] setImage:image];
//得到視圖的left top的X,Y坐標點
#define VIEW_TX(view) (view.frame.origin.x)
#define VIEW_TY(view) (view.frame.origin.y)
//得到視圖的right bottom的X,Y坐標點
#define VIEW_BX(view) (view.frame.origin.x + view.frame.size.width)
#define VIEW_BY(view) (view.frame.origin.y + view.frame.size.height )
//得到視圖的尺寸:寬度憾股、高度
#define VIEW_W(view) (view.frame.size.width)
#define VIEW_H(view) (view.frame.size.height)
//得到frame的X,Y坐標點
#define FRAME_TX(frame) (frame.origin.x)
#define FRAME_TY(frame) (frame.origin.y)
//得到frame的寬度鹿蜀、高度
#define FRAME_W(frame) (frame.size.width)
#define FRAME_H(frame) (frame.size.height)
//----------------------視圖相關(guān)----------------------------
//---------------------打印日志--------------------------
//Debug模式下打印日志,當前行,函數(shù)名
#if DEBUG
#define DLog(FORMAT, ...) fprintf(stderr,"\nfunction:%s line:%d content:%s\n", __FUNCTION__, __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
#define NSLog(FORMAT, ...) nil
#endif
//Debug模式下打印日志,當前行,函數(shù)名 并彈出一個警告
#ifdef DEBUG
# define WDLog(fmt, ...) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%s\n [Line %d] ", __PRETTY_FUNCTION__, __LINE__] message:[NSString stringWithFormat:fmt, ##__VA_ARGS__] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; }
#else
# define NSLog(...)
#endif
//打印Frame
#define LogFrame(frame) NSLog(@"frame[X=%.1f,Y=%.1f,W=%.1f,H=%.1f",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height)
//打印Point
#define LogPoint(point) NSLog(@"Point[X=%.1f,Y=%.1f]",point.x,point.y)
//---------------------打印日志--------------------------
預(yù)編譯指令還有很多玩法箕慧,以后補充了。