How to print out the method name and line number and conditionally disable NSLog?(如何打印出方法名称和行号并有条件地禁用 NSLog?)
问题描述
我正在做一个有关在 Xcode 中调试的演示文稿,并希望获得有关有效使用 NSLog 的更多信息.
I'm doing a presentation on debugging in Xcode and would like to get more information on using NSLog efficiently.
特别是,我有两个问题:
In particular, I have two questions:
- 有没有办法轻松 NSLog 当前方法的名称/行号?
- 有没有办法在编译发布代码之前轻松禁用"所有 NSLog?
推荐答案
下面是一些我经常使用的关于 NSLog 的有用宏:
Here are some useful macros around NSLog I use a lot:
#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__)
DLog 宏仅用于在设置了 DEBUG 变量时输出(项目的 C 标志中的 -DDEBUG 用于调试配置).
The DLog macro is used to only output when the DEBUG variable is set (-DDEBUG in the projects's C flags for the debug confirguration).
ALog 将始终输出文本(如常规 NSLog).
ALog will always output text (like the regular NSLog).
输出(例如 ALog(@"Hello world") )将如下所示:
The output (e.g. ALog(@"Hello world") ) will look like this:
-[LibraryController awakeFromNib] [Line 364] Hello world
这篇关于如何打印出方法名称和行号并有条件地禁用 NSLog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何打印出方法名称和行号并有条件地禁用 NSLog?


基础教程推荐
- 如何从 logcat 中删除旧数据? 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01