In C# I can use the following code to have code which only executes during debug build, how can I do the same in Xcode?
#if DEBUG { // etc etc } #endif
Using a print statement might be the simplest way to debug code. All programming languages have one or more commands that you can use to print values out on the console when the software is running.
This example teaches you how to debug code in Excel VBA. By pressing F8, you can single step through your code. The is very useful because it allows you to see the effect of each code line on your worksheet.
To help us in our fight against bugs, debuggers were developed. They are nothing more than programs that are able to read other programs and go through them line by line, checking whatever information we want along the way (such as the value of variables, for example). The first example we're going to see is Visual Studio debugger.
It's a mechanism that helps us save energy and do things quicker. But when debugging, we need to enforce our brain to work with us and be as present as possible on every line of code. As your codebase get's bigger, it will be hard to analyze every line of code in the search for your bug.
You can use
#ifdef DEBUG .... #endif
You'll need to add DEBUG=1
to the project's preprocessor symbol definitions in the Debug configuration's settings as that's not done for you automatically by Xcode.
I personally prefer doing DEBUG=1
over checking for NDEBUG=0
, since the latter implies that the default build configuration is with debug information which you then have to explicitly turn off, whereas 'DEBUG=1' implies turning on debug only code.
The NDEBUG symbol should be defined for you already in release mode builds
#ifndef NDEBUG /* Debug only code */ #endif
By using NDEBUG you just avoid having to specify a -D DEBUG argument to the compiler yourself for the debug builds
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With