iOS:用于调试构建的可选代码片段

发布于 2025-01-01 05:48:34 字数 111 浏览 5 评论 0原文

对于我的应用程序,我希望有一个调试视图,我只想在调试版本中而不是在 发布版本。但我不想更改我的代码。这就是为什么我想知道我是否可以 检查一些编译器标志(如果这是发布版本)并排除一些我只想的代码 用于调试版本。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月亮是我掰弯的 2025-01-08 05:48:34

在您的项目构建设置中,查找预处理器定义部分,您可以在其中仅在调试构建中定义一个变量,例如 DEBUG=1,然后在代码中使用它:

#if DEBUG
    NSLog(@"This will only print in debug!");
#endif

只需确保在您的发布配置中,相同的定义在构建设置的相同位置设置为 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:

#if DEBUG
    NSLog(@"This will only print in debug!");
#endif

Just make sure in your release configuration, that same define is set to 0 in the same location in your build settings

温柔女人霸气范 2025-01-08 05:48:34

检查您的项目构建设置以进行调试,以确保设置了“DEBUG”(Apple 免费为您提供此设置)- 通过选择项目并单击构建设置选项卡来执行此操作。搜索“DEBUG”并查看是否确实设置了 DEBUG。

我的项目设置的构建设置选项卡

然后在源文件中有条件地进行调试代码

#ifdef DEBUG

// Something to log your data here or even add a whole subview to see it on the device

#else

// 

#endif

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.

Build Settings tab of my project settings

then conditionally code for DEBUG in your source files

#ifdef DEBUG

// Something to log your data here or even add a whole subview to see it on the device

#else

// 

#endif
小瓶盖 2025-01-08 05:48:34

除了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文