Swift:
Using a debugger

How to:

To use the debugger in Xcode (the IDE for Swift), you can set breakpoints, inspect variables, and watch expressions. Here’s an example:

func findFactorial(of number: Int) -> Int {
    if number == 0 {
        return 1
    }
    return number * findFactorial(of: number - 1)
}

let result = findFactorial(of: 5)
print(result)

Set a breakpoint by clicking left to a line number in Xcode, and run the program. When it hits the breakpoint, Xcode pauses the execution. Now you can:

  1. Check variable values.
  2. Step over (run the next line) or step into (go inside a function) using the debugger controls.
  3. Add expressions to the ‘watch list’ to monitor changes to specific variables or constants.

Here’s what you might see in the debug area:

(lldb) po number
5
(lldb) po result
120

Deep Dive:

Debuggers have been part of the programming landscape since the 1940s, evolving from simple breakpoint systems to complex, UI-driven experiences. Other options besides Xcode’s built-in debugger include third-party tools like LLDB (Low Level Debugger) which Xcode uses under the hood. Some folks even debug with print() statements (affectionately known as “caveman debugging”), but this is less efficient for large projects or complex bugs. When you use a debugger, you’re juggling execution control, runtime introspection, and data manipulation. A deep understanding of these principles goes a long way in efficient debugging.

See Also: