Debugging and Instruments

IOS Development 12 min min read Updated: Nov 16, 2026 Intermediate
Debugging and Instruments
Intermediate Topic 3 of 5

1. Print and Logger

swift print("Loaded \(items.count) items") import OSLog let logger = Logger(subsystem: "com.yourname.myapp", category: "network") logger.info("Request started") logger.error("Failed: \(error.localizedDescription, privacy: .public)")

Logger messages can be filtered in the Xcode console and in the Console app, and they do not slow down release builds.

2. Breakpoints

Click the line number gutter to set a breakpoint. When the app reaches that line it pauses and you can inspect all variables.

ControlShortcutAction
ContinueCtrl + Cmd + YRun until the next breakpoint
Step overF6Run this line
Step intoF7Go inside the function
Step outF8Finish the function
  • Conditional breakpoint: Right-click the breakpoint, choose Edit, set a condition like index == 42
  • Action breakpoint: log a message and continue automatically, without editing code
  • Exception breakpoint: Add from the Breakpoint navigator to stop exactly where a crash starts
  • Symbolic breakpoint: stop whenever a certain method is called

3. LLDB in the Console

text (lldb) po user print an object's description (lldb) p count print a value (lldb) expression count = 99 change a value while paused (lldb) bt show the call stack (backtrace) (lldb) frame variable list all local variables

4. View Debugger

While the app runs, click Debug View Hierarchy (the stacked-layers icon). Xcode shows a 3D exploded view of every layer. Use it to find hidden views, wrong sizes or overlapping buttons.

5. Understanding Crashes

MessageUsual cause
Unexpectedly found nil while unwrapping an OptionalUsed ! on nil
Index out of rangeArray access with a bad index
Thread 1: Fatal error: No ObservableObject / environment object of type ... foundForgot to inject a model with .environment(...)
Unable to simultaneously satisfy constraintsConflicting Auto Layout rules
This application is modifying the autolayout engine from a background threadUI updated off the main thread
Terminating app due to uncaught exceptionMissing Info.plist usage description or an Objective-C error

6. Memory Leaks: Retain Cycles

swift class Screen { var onFinish: (() -> Void)? func setup() { onFinish = { self.cleanup() } // LEAK: self holds the closure, the closure holds self onFinish = { [weak self] in self?.cleanup() } // FIX } func cleanup() {} }

To find leaks run the app, open the screen and leave it several times, then click the Debug Memory Graph button. Leaked objects appear with a purple exclamation mark.

7. Instruments

Open Instruments with Product > Profile (Cmd + I). Choose a template:

TemplateFinds
Time ProfilerSlow functions that use the CPU
Allocations / LeaksMemory growth and leaks
SwiftUIViews that redraw too often
NetworkSlow or repeated requests
Energy LogBattery drain
Profile on a real device in Release mode: The Simulator uses your Mac's powerful hardware and Debug builds are slower, so measurements are not realistic.
Debug method: Reproduce the bug, make the smallest example, check your assumptions with a breakpoint, fix one thing, and then add a test so the bug never returns.

Conclusion

Debugging is a skill. Use Logger, breakpoints, LLDB, the view debugger, the memory graph and Instruments, in that order of effort.

Quick Summary: Pause with breakpoints, inspect with LLDB, find leaks with the memory graph and measure performance with Instruments on a real device.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators