Master Xcode Debugging: Essential Tips for iOS Developers
Learn essential Xcode debugging techniques for iOS development, covering project creation, interface overview, running apps, various breakpoint types, common LLDB commands, multithreading strategies, UI inspection tools, crash analysis, and recommended resources, all illustrated with step‑by‑step screenshots.
1. Basic Usage
How to create a new Xcode project: File → New → Project, choose Single View Application, name it, set the location, and create.
Brief overview of the Xcode interface: navigation area, editor area, debug area, utilities area, and toolbar.
Run the app on a simulator or a real device by clicking the Run (triangle) button.
2. Breakpoints
Xcode supports several breakpoint types: standard, conditional, symbolic, exception, watch, and thread breakpoints.
Standard breakpoints can be added by clicking the gutter or using Command+\. They stop execution on the selected line.
Conditional breakpoints add an expression; the breakpoint triggers only when the condition evaluates to true. Edit a breakpoint via right‑click → Edit Breakpoint.
Symbolic breakpoints target a specific function or method name (Objective‑C or C++). Add one via the Breakpoint navigator → + → Add Symbolic Breakpoint.
Exception breakpoints stop when an exception is thrown. Add via Breakpoint navigator → + → Add Exception Breakpoint.
Watch breakpoints trigger when a variable’s value changes. Add by right‑clicking a variable in the Watch window → Watch " variable ".
Thread breakpoints stop only on a specific thread ID, useful for multithreaded debugging.
Breakpoint actions let you run debugger commands, AppleScript, or shell scripts without changing source code.
3. Common Commands
Use p to print primitive values and po to print Objective‑C objects. expr evaluates an expression and can modify variables at runtime. call invokes a function without recompiling. image lists loaded modules; image lookup –address <addr> finds the source line for a given address.
btand btall display the current thread’s stack trace or all threads’ stack traces.
4. Multithreading
Multithreaded bugs may disappear when breakpoints are set because the debugger changes execution order. Use log statements, conditional breakpoints, and thread breakpoints to isolate timing‑related issues.
5. UI Debugging
Inspect UI elements with p, po, or expr to modify properties on the fly. Print the view hierarchy using po [view recursiveDescription] or use visual tools such as Reveal.
Xcode 5 provides quick variable preview: hover over a variable and click the preview button.
Set symbolic breakpoints on UI methods (e.g., setTitle of UIButton) to track unexpected UI changes.
6. Other Tips
Enable exception breakpoints to catch crashes caused by exceptions. Use Instruments’ Zombies template to detect use‑after‑free errors; if Zombies is unavailable, refer to LLDB techniques for diagnosing messages sent to deallocated instances.
Recommended reading: the original "debugging" book, its Chinese translation, and Facebook’s open‑source Chisel debugging plugin.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
WeChat Client Technology Team
Official account of the WeChat mobile client development team, sharing development experience, cutting‑edge tech, and little‑known stories across Android, iOS, macOS, Windows Phone, and Windows.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
