[Cocoa]XCode中定制Prefix.pch文件
XCode中定制Prefix.pch文件
羅朝輝 (http://www.rzrgm.cn/kesalin/)
本文遵循“署名-非商業用途-保持一致”創作公用協議
擴展名 pch 表示 “precompliled header”,即預編譯頭文件,prefix.pch 為 XCode 工程默認生成的預編譯頭文件,在其中我們可以定制一些全局的宏,以方便開發。
下面貼一段來自 Cocoa Is My Girlfriend 定制的宏:
#ifdef DEBUG
#define DLog(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__])
#define ALog(...) [[NSAssertionHandler currentHandler] handleFailureInFunction:[NSString stringWithCString:__PRETTY_FUNCTION__ encoding:NSUTF8StringEncoding] file:[NSString stringWithCString:__FILE__ encoding:NSUTF8StringEncoding] lineNumber:__LINE__ description:__VA_ARGS__]
#else
#define DLog(...) do { } while (0)
#ifndef NS_BLOCK_ASSERTIONS
#define NS_BLOCK_ASSERTIONS
#endif
#define ALog(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__])
#endif
#define ZAssert(condition, ...) do { if (!(condition)) { ALog(__VA_ARGS__); }} while(0)
詳細解說請閱讀原文,在這里我只說明其用法:
1,如果是 debug 模式下,需要在編譯選項 Preprocessor Macros 中設置 DEBUG 宏;
2,DLog 相當于 NSLog,但只在 debug 模式下有效;在 release 模式下,它什么也不做;
3,ALog 是 Assert Log 的簡寫,在 debug 模式下,相當于強制 assert;在 release 模式下,相當于 NSLog;
4,ZAssert 是帶有條件判斷的 ALog;
參考資料:
My current Prefix.pch file
浙公網安備 33010602011771號