UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Basic Swift debugging using print()

We're going to start with the absolute easiest debugging technique, which is the print() function. This prints a message into the Xcode debug console that can say anything you want, because users won't see it in the UI. The "scattershot" approach to bug fixing is to litter your code with calls to print() then follow the messages to see what's going on.

You'll meet lots of people telling you how bad this is, but the truth is it's the debugging method everyone starts with – it's easy, it's natural, and it often gives you enough information to solve your problem. Use it with Swift's string interpolation to see the contents of your variables when your app is running.

We’ve used print() several times already, always in its most basic form:

print("I'm inside the viewDidLoad() method!")

By adding calls like that to your various methods, you can see exactly how your program flowed.

However, print() is actually a bit more complicated behind the scenes. For example, you can actually pass it lots of values at the same time, and it will print them all:

print(1, 2, 3, 4, 5)

That makes print() a variadic function, which you learned about previously. Here, though, it’s worth adding that print()’s variadic nature becomes much more useful when you use its optional extra parameters: separator and terminator.

The first of these, separator, lets you provide a string that should be placed between every item in the print() call. Try running this code:

print(1, 2, 3, 4, 5, separator: "-")

That should print “1-2-3-4-5”, because the separator parameter is used to split up each item passed into print().

The second optional parameter, terminator, is what should be placed after the final item. It’s \n by default, which you should remember means “line break”. If you don’t want print() to insert a line break after every call, just write this:

print("Some message", terminator: "")

Notice how you don’t need to specify separator if you don’t want to.

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.2/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.