One of the most important skills every programmer needs to have is debugging! Ever since I’ve been working with Swift, I’ve been definitely off in my coding because Swift is different to debug than my usual Objective-C debugging flow.

So last week, I finally got around to watching the Advanced Swift Debugging in LLDB WWDC14 video. I really wish I watched it a lot earlier! I also got some tips from a few people, which have been very helpful.

Here are some tips from my new Swift debugging flow:

What Type is It Anyway?

Type inference is nice while you’re writing the code, but what type a variable or constant is can definitely be confusing looking back at your own code or at someone else’s. In addition, Swift types are different than Objective-C types (String vs NSString for example), so that can confuse things further.

The coolest trick I’ve found is when you Option + Click on the variable name, you can see what type it is:

In the above example, I now know that my quotes variable is an array of Quote objects, which might not be as clear on first glance.

The Debug Area

I’m a huge fan of breakpoints. With Swift, when you put in a breakpoint, the type of each variable at the time of the breakpoint is evaluated:

Putting in a breakpoint and seeing all the evaluated types is a great way to get a bird’s eye view of what you’re playing with.

Print Variable

As you can see in the debug view above, the indexPath variable does not give me the information I need – the actual indexPath! There are several ways to print out the variable.

The one I’m starting to use a bit more often is selecting the variable I want to look at, and clicking the little i icon at the bottom left of the debugger:

Although this hasn’t worked for me well in previous betas, looks like you can also use the p or po command in the debugger to print out your Swift variable:

Notice that po printed out the description of the object, which included the value of the indexPath, while p printed out the more raw representation of the object, including it’s location in memory.

Frame Variable

Finally, you can also use the fr v (frame variable) command to print out your variables. In the Advanced Swift Debugging in LLDB video, they walk you through adding arguments to this command. It can get pretty complicated! I’ll stick to the simple stuff though:

The -R argument prints out the Raw output of the variable. Notice that in this case, it’s printing out the location in memory of the object, not the actual value as po or the i icon does.

Do you have additional Swift debugging tips? Let me know in the comments!

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.