如何打印出方法名和行號,并有條件禁用NSlog?我正在做一個關(guān)于Xcode調(diào)試的演示,希望獲得更多關(guān)于如何高效使用NSLog的信息。特別是,我有兩個問題:是否有一種方法可以輕松地記錄當(dāng)前方法的名稱/行號?在編譯發(fā)布代碼之前,是否有一種方法可以輕松地“禁用”所有NSLogs?
3 回答

縹緲止盈
TA貢獻(xiàn)2041條經(jīng)驗 獲得超4個贊
#ifdef DEBUG# define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)#else# define DLog(...)#endif// ALog always displays output regardless of the DEBUG setting#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
-[LibraryController awakeFromNib] [Line 364] Hello world

鳳凰求蠱
TA貢獻(xiàn)1825條經(jīng)驗 獲得超4個贊
DLog
ALog
ULog
UIAlertView
DLog
輸出會像 NSLog
只有在設(shè)置了調(diào)試變量時 ALog
總是輸出類似 NSLog
ULog
將顯示 UIAlertView
只有在設(shè)置了調(diào)試變量時
#ifdef DEBUG # define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); #else # define DLog(...) #endif #define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); #ifdef DEBUG # 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

大話西游666
TA貢獻(xiàn)1817條經(jīng)驗 獲得超14個贊
NSLog(@"%s %d %s %s", __FILE__, __LINE__, __PRETTY_FUNCTION__, __FUNCTION__);
/proj/cocoa/cdcli/cdcli.m 121 managedObjectContext managedObjectContext
__FUNCTION__
__PRETTY_FUNCTION__
#define NSLog
- 3 回答
- 0 關(guān)注
- 552 瀏覽
添加回答
舉報
0/150
提交
取消