iOS:用于调试构建的可选代码片段
对于我的应用程序,我希望有一个调试视图,我只想在调试版本中而不是在 发布版本。但我不想更改我的代码。这就是为什么我想知道我是否可以 检查一些编译器标志(如果这是发布版本)并排除一些我只想的代码 用于调试版本。
for my App I'd like to have a debug view that I want to have only in debug-builds and not in
release builds. I don't want to change my code though. Thats why i am wondering if I can
check some compiler flag if this is a release build and exclude some code that i only want to
have for debug builds.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的项目构建设置中,查找预处理器定义部分,您可以在其中仅在调试构建中定义一个变量,例如
DEBUG=1
,然后在代码中使用它:只需确保在您的发布配置中,相同的定义在构建设置的相同位置设置为 0
In your projects build settings, look for the preprocessor defines section, in there you can define a variable in your debug build only, such as
DEBUG=1
, and then use this in your code:Just make sure in your release configuration, that same define is set to 0 in the same location in your build settings
检查您的项目构建设置以进行调试,以确保设置了“DEBUG”(Apple 免费为您提供此设置)- 通过选择项目并单击构建设置选项卡来执行此操作。搜索“DEBUG”并查看是否确实设置了 DEBUG。
然后在源文件中有条件地进行调试代码
Check your projects build settings for debug to ensure that 'DEBUG' is being set - Apple gives you this for free - do this by selecting the project and clicking on the build settings tab. Search for 'DEBUG' and look to see if indeed DEBUG is being set.
then conditionally code for DEBUG in your source files
除了 Damo 解释的构建设置之外,还有一个等效的构建设置,我已经看到它被使用并且也有效。在“其他 C 标志”(OTHER_CFLAGS) 中,将“-DDEBUG”添加到“调试”配置中。
In addition to the build setting explained by Damo, there is an equivalent Build Setting which I have seen used and also works. In Other C Flags (OTHER_CFLAGS), add
-DDEBUG
to the Debug configuration.