常用的PCH
- pch頭文件的內(nèi)容能被項(xiàng)目中的其他所有源文件共享和訪問
** 注意: PCH文件的特點(diǎn), 項(xiàng)目中的所有其他代碼文件無需顯示導(dǎo)入該P(yáng)CH文件, 默認(rèn)就都可以訪問(其他文件無需手動(dòng)#import該 pch文件就能使用)。
- 一般在pch文件中定義一些全局的宏
- 在pch文件中添加下列預(yù)處理指令,然后在項(xiàng)目中使用Log(…)來輸出日志信息松却,就可以在發(fā)布應(yīng)用的時(shí)候谎砾,一次性將NSLog語句移除(在調(diào)試模式下,才有定義DEBUG)
#ifdef __OBJC__
// 自定義控制臺(tái)打印
#ifdef DEBUG
#define RZLog(...) NSLog(__VA_ARGS__)
#else
#define RZlog(...)
#endif
#define kScreenSize [UIScreen mainScreen].bounds
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#endif
修改加載pch地址
- 選中項(xiàng)目 -> Build Setting -> All -> 搜索"prefix head" -> 修改Prefix Header的內(nèi)容為:
-
$(SRCROOT)/$(PRODUCT_NAME)/PrefixHeader.pch
(如果有問題,換下面的方式,可能會(huì)與中文有關(guān)) $(SRCROOT)/對(duì)應(yīng)的文件夾名/PrefixHeader.pch
-
工具類的宏
#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#ifdef DEBUG
#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define DLog(...)
#endif
#ifdef DEBUG
#define ULog(...)
//#define ULog(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 ULog(...)
#endif
//System version utils
#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)
// 獲取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 IsPortrait ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
#define IsNilOrNull(_ref) (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]))
//角度轉(zhuǎn)弧度
#define DEGREES_TO_RADIANS(d) (d * M_PI / 180)
//大于等于7.0的ios版本
#define iOS7_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")
//大于等于8.0的ios版本
#define iOS8_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")
//iOS6時(shí),導(dǎo)航VC中view的起始高度
#define YH_HEIGHT (iOS7_OR_LATER ? 64:0)
//獲取系統(tǒng)時(shí)間戳
#define getCurentTime [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]]