Keep your debug toolkit handy but hidden using conditional compilation
Tags: ios, swift, objc
Published: 2019-03-08 14:20:44

---
title: Keep your debug toolkit handy but hidden using conditional compilation
published: true
description: You want to keep your debug code around, but you don't want to ship it either. Conditional compilation is a way to accomplish both.
canonical_url: https://www.appsdissected.com/hide-debug-code-conditional-compilation/
tags: ios,swift,objc
---
When you’ve been working on an app for a while, especially as it gets large, you often start to include snippets of code in the app that have been useful for debugging your app in the past. Such debugging code may be as simple as log statements, to as complex as adding full screens to your interface, activated by controls that you add to your interface for debugging only.

Such tools become an integral part of your debugging environment; you don’t want to throw them out. <strong>But do you really want to ship those in your app?</strong> It makes your app bigger and slower. You could comment them out before you release, but after a marathon debug session, do you trust that you’ll comment out again <strong>all</strong> of the debugging tools you enabled?

<strong>How can I keep my debugging addons, but ensure they aren’t in the version I deploy to the App Store?</strong>

There is a way: you can use a feature of Swift called conditional compilation to enable debugging code on your debug build. If you still have Objective-C code, you’re still in luck: a similar feature called preprocessor macros can provide the same functionality.

<hr>

We’ll start with the happy path. If you’ve created your project with a recent version of Xcode (at least 9.3, so within the past year or so), you’re already set, and you can skip down to the part where we talk about the code itself (or you can go through it to verify your settings are there).

If you have an older project, you’ll need to add a couple of build settings for the following code to work:

<img class="alignnone size-full wp-image-195" src="https://www.appsdissected.com/wp-content/uploads/2019/02/Screen-Shot-2019-02-28-at-19.03.57.png" width="2292" height="200">

Open your project, switch to the Build Settings tab, show All build settings, and select Levels.

<img class="alignnone size-full wp-image-196" src="https://www.appsdissected.com/wp-content/uploads/2019/02/Screen-Shot-2019-02-28-at-18.52.35.png" width="1726" height="214">

Find Active Compilation Conditions, and add <code>DEBUG</code> under the project setting, as above.

<img class="alignnone size-full wp-image-194" src="https://www.appsdissected.com/wp-content/uploads/2019/02/Screen-Shot-2019-02-28-at-18.53.12.png" width="1726" height="284">

If you have Objective-C code as well, find Preprocessor Macros and add <code>DEBUG=1</code> to the project setting.

Once you have those set, you’re ready to hide your debug code.

Even though the two features have different names, compilation conditions and preprocessor macros, the code to wrap your debug code with is exactly the same, and it’s really easy:
<pre><code>#if DEBUG
   // My debug code is here
#endif
</code></pre>
When you do your builds for the simulator and your development devices, all of your debug code will be enabled, ready for you to use. When you build an archive for App Store release or TestFlight, DEBUG will not be defined, and all of that code will disappear, ensuring your app stays fast and lean.

You can keep all your debugging addons AND keep them to yourself.

<hr />

Did you like this tip? The next tip on <a href="https://www.appsdissected.com/slow-laggy-xcode-memory/">your Xcode lag not being (entirely) Xcode's fault</a> is already waiting for you.

Or <a href="https://www.appsdissected.com/newsletter/">sign up to get every tip straight to your inbox</a>, plus newsletter-only tips and deeper dives on website articles and discounts on future product launches.

This post originally published at <a href="https://www.appsdissected.com/hide-debug-code-conditional-compilation/">Apps Dissected.</a>