Xcode打印方法NSLog() 在Xcode8以后就出現(xiàn)了打印輸出不完整、打印中文時(shí)候顯示的問題,調(diào)試是在難受。
之前也用過printf替換NSLog,但是還是中文顯示不了,于是用分類,就是重新寫一個(gè)字典和數(shù)組的分類,重寫他們的- (NSString *)descriptionWithLocale:(id)locale這個(gè)方法,搞得很麻煩?,F(xiàn)在一個(gè)宏搞定,簡單拖到pch頭文件里就行。
功能:
在debug模式打印、release模式下不打印。
解決打印輸出不完整,打印中文顯示Unicode的問題。
由于里面用了NSString,如果前面沒有添加其他頭文件里包含,就可能報(bào)錯,所以在pch文件里需要加:#import <UIKit/UIKit.h>
廢話不多說,Here is the code:
//修復(fù)打印不完整,打印中文顯示Unicode碼問題
#ifndef __OPTIMIZE__
#define NSLog(FORMAT, ...)? fprintf(stderr, "%s [%s-%d] %s\n", [[NSDate br_stringFromDate:[NSDate date] dateFormat:@"yyyy-MM-dd HH:mm:ss.SSSS"] UTF8String], [[[NSString stringWithUTF8String: __FILE__] lastPathComponent] UTF8String], __LINE__, [[[NSString alloc] initWithData:[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] dataUsingEncoding:NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding] UTF8String]?[[[NSString alloc] initWithData:[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] dataUsingEncoding:NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding] UTF8String]:[[NSString stringWithFormat: FORMAT, ## __VA_ARGS__] UTF8String]);
#else
#define NSLog(FORMAT, ...) nil
#endif