在開(kāi)發(fā)中,我們經(jīng)常需要使用一些宏或者自定義一些宏。
宏是已被命名的代碼片段。使用宏時(shí)會(huì)進(jìn)行代碼段的替換。有兩種宏的類(lèi)型,一種是類(lèi)對(duì)象的宏,封裝使用的數(shù)據(jù)對(duì)象,另一種是類(lèi)函數(shù)的宏,封裝函數(shù)的調(diào)用。
以下下是一些C語(yǔ)言常用的宏命令
#define 定義一個(gè)預(yù)處理宏
#undef 取消宏的定義
#include 包含文件命令
#include_next 與#include相似, 但它有著特殊的用途
#if 編譯預(yù)處理中的條件命令, 相當(dāng)于C語(yǔ)法中的if語(yǔ)句
#ifdef 判斷某個(gè)宏是否被定義, 若已定義, 執(zhí)行隨后的語(yǔ)句
#ifndef 與#ifdef相反, 判斷某個(gè)宏是否未被定義
#elif 若#if, #ifdef, #ifndef或前面的#elif條件不滿(mǎn)足, 則執(zhí)行#elif之后的語(yǔ)句, 相當(dāng)于C語(yǔ)法中的else-if
#else 與#if, #ifdef, #ifndef對(duì)應(yīng), 若這些條件不滿(mǎn)足, 則執(zhí)行#else之后的語(yǔ)句, 相當(dāng)于C語(yǔ)法中的else
#endif #if, #ifdef, #ifndef這些條件命令的結(jié)束標(biāo)志.
#defined 與#if, #elif配合使用, 判斷某個(gè)宏是否被定義
#line 標(biāo)志該語(yǔ)句所在的行號(hào)
# 將宏參數(shù)替代為以參數(shù)值為內(nèi)容的字符竄常量
## 將兩個(gè)相鄰的標(biāo)記(token)連接為一個(gè)單獨(dú)的標(biāo)記
#pragma 說(shuō)明編譯器信息
#warning 顯示編譯警告信息
#error 顯示編譯錯(cuò)誤信息
宏的一些系統(tǒng)工具方法
...:可變參數(shù)
__COUNTER__ 無(wú)重復(fù)的計(jì)數(shù)器,從程序啟動(dòng)開(kāi)始每次調(diào)用都會(huì)++,常用語(yǔ)宏中定義無(wú)重復(fù)的參數(shù)名稱(chēng)
__FILE__:當(dāng)前文件的絕對(duì)路徑,常見(jiàn)于log中
__LINE__:展開(kāi)該宏時(shí)在文件中的行數(shù),常見(jiàn)于log中
__func__:所在scope的函數(shù)名稱(chēng),常見(jiàn)于log中
__DATE__: “替代文字”是一個(gè)含有編譯日期的字符串字面值,日期格式為“mm dd yyyy”(例如:“Mar 19 2006”)。如果日期小于10日,就在日的前面放一個(gè)空格符。NSLog(@"_DATE_=%s",__DATE__);
__FUNCTION__:當(dāng)前函數(shù)名稱(chēng)
__TIME__: 此字符串字面值包含編譯時(shí)間,格式為“hh:mm:ss”(范例:“08:00:59”)。
__STDC__:整數(shù)常量1,表示此編譯器遵循ISOC標(biāo)準(zhǔn)。
iOS 系統(tǒng)中定義了一些宏用于版本控制
__IPHONE_OS_VERSION_MIN_REQUIRED //當(dāng)前app支持運(yùn)行的最低版本
__IPHONE_OS_VERSION_MAX_ALLOWED //當(dāng)前app支持的最高版本
#define __IPHONE_7_0 70000
#define __IPHONE_7_1 70100
#define __IPHONE_8_0 80000
#define __IPHONE_8_1 80100
#define __IPHONE_8_2 80200
#define __IPHONE_8_3 80300
#define __IPHONE_8_4 80400
#define __IPHONE_9_0 90000
#define __IPHONE_9_1 90100
#define __IPHONE_9_2 90200
常用的一些自定義宏整理:
- 設(shè)備相關(guān)的宏
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define CurrentSystemVersion ([[UIDevice currentDevice] systemVersion])
#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
//大于等于7.0的ios版本
#define iOS7_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")
#define IsNilOrNull(_ref) (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]))
//檢查系統(tǒng)版本
#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)
- UI常用宏
#define UI_NAVIGATION_BAR_HEIGHT 44
#define UI_TOOL_BAR_HEIGHT 44
#define UI_TAB_BAR_HEIGHT 49
#define UI_STATUS_BAR_HEIGHT 20
#define UI_SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define UI_SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
- 用來(lái)整體區(qū)分的宏
/* ****** release時(shí),屏蔽NSLog ****** */
#if defined (DEBUG) && DEBUG == 1
#else
#define NSLog(...) {};
#endif
//對(duì)log加上一些參數(shù)
#ifdef DEBUG
# define DLog(...) NSLog((@"%s [Line %d] %@"), __PRETTY_FUNCTION__, __LINE__, [NSString stringWithFormat:__VA_ARGS__])
# define SLog(...) NSLog(__VA_ARGS__)
#else
# define DLog(...)
# define SLog(...)
#endif
/* ****** 根據(jù)是否使用ARC做不同操作 ****** */
#if __has_feature(objc_arc)
//compiling with ARC
#else
// compiling without ARC
#endif
/* ****** 區(qū)分模擬器和真機(jī) ****** */
#if TARGET_OS_IPHONE
//iPhone Device
#endif
#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif
- 初始化顏色的宏
// rgb顏色轉(zhuǎn)換(16進(jìn)制->10進(jìn)制)
#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 kPathTemp NSTemporaryDirectory()
#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
#define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]
#define kPathSearch [kPathDocument stringByAppendingPathComponent:@"Search.plist"]#define kPathMagazine [kPathDocument stringByAppendingPathComponent:@"Magazine"]
#define kPathDownloadedMgzs [kPathMagazine stringByAppendingPathComponent:@"DownloadedMgz.plist"]
#define kPathDownloadURLs [kPathMagazine stringByAppendingPathComponent:@"DownloadURLs.plist"]
#define kPathOperation [kPathMagazine stringByAppendingPathComponent:@"Operation.plist"]
#define kPathSplashScreen [kPathCache stringByAppendingPathComponent:@"splashScreen"]
- 一些宏函數(shù)
#define degreesToRadian(x) (M_PI * (x) / 180.0) //弧度轉(zhuǎn)角度
#define radianToDegrees(radian) (radian*180.0)/(M_PI) //角度轉(zhuǎn)弧度
#pragma mark - 單例
#define DeclareSingletonInterface(className) \
+(classname *)shared##classname
#define ImplementSingletonInterface(className) \
+(classname *)shared##classname { \
static dispatch_once_t onceToken = 0; \
static id sharedInstance = nil; \
dispatch_once(&onceToken, ^{ \
sharedInstance = [[self alloc] init]; \
}); \
return sharedInstance; \
}
對(duì)宏使用介紹比較詳細(xì)的一篇文章宏定義的黑魔法 - 宏菜鳥(niǎo)起飛手冊(cè),通過(guò)對(duì)min函數(shù)的宏定義一個(gè)逐次完善的過(guò)程來(lái)講解宏定義。