iOS 開(kāi)發(fā)中使用一些常用宏定義可以大大提高開(kāi)發(fā)效率,提高代碼的重用性.
以下是一些常用的宏定義:
//屏幕寬、高
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
//1個(gè)像素的寬度
#define SINGLE_LINE_WIDTH (1.0f/[UIScreen mainScreen].scale)
//系統(tǒng)版本
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
//圖片
#define ImageWithName(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]]
//rgb顏色(十進(jìn)制)
#define UIColorFromRGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
//rgb顏色(十六進(jìn)制)
#define UIColorFromHexValue(HexValue) [UIColor colorWithRed:((float)((HexValue & 0xFF0000) >> 16))/255.0 green:((float)((HexValue & 0xFF00) >> 8))/255.0 blue:((float)(HexValue & 0xFF))/255.0 alpha:1.0]
//將對(duì)象轉(zhuǎn)換成弱引用類型猪勇,有block時(shí)使用
#define WeakObj(obj) __block __weak typeof(obj) weak_##obj = obj
//document目錄
#define DOCUMENT NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject
//DEBUG模式下,打印日志(包括函數(shù)名颠蕴、行號(hào))
#ifdef DEBUG
# define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
# define DLog(...)
#endif
像這些宏定義,在工程中全局都需要經(jīng)常使用,我們可以把它放入工程中的PrefixHeader.pch
文件中.具體步驟如下:
- 新建
PrefixHeader.pch
文件,如圖:
- 在
PrefixHeader.pch
中添加我們需要使用的宏定義,如圖:
-
在 Prefix Header 設(shè)置路徑, 具體位置在 TARGET ->Build Settings -> Prefix Header 中,如圖:
這樣我們的 pch 文件就添加完成了, 工程中全局就可以使用這些宏定義了.