Back to course overview
Module 6Testing & debugging with AI 14 min

Reading crashes and fixing bugs with AI

A repeatable method for turning a scary crash log into a fixed bug, using AI as a diagnostic partner.

Every app crashes during development. The skill isn't avoiding crashes — it's reading them calmly. AI is excellent at interpreting crash logs if you give it the right slice of information.

The debugging loop

  1. Reproduce — find the exact tap sequence that triggers it. A bug you can reproduce is a bug you can fix.
  2. Read the top of the stack — Xcode highlights the line that crashed and prints a reason in the console. Read the first meaningful line, not the whole wall of text.
  3. Isolate — note the file, line, and the values involved (Xcode's debugger shows variables).
  4. Ask precisely — give AI the error, the crashing function, and what you were doing.

The most common Swift crash

swiftswift
// Console: "Fatal error: Unexpectedly found nil while unwrapping an Optional value"
let title = entry.aiTitle!   // <- the "!" forces an optional; crashes when it's nil

The ! says "I promise this is never nil." When that promise is false — an entry without insights yet — the app dies. The fix is to handle the nil case honestly:

swiftswift
let title = entry.aiTitle ?? "Untitled"   // provide a fallback instead of crashing
Prompt to try

My iOS app crashes with "Fatal error: Unexpectedly found nil while unwrapping an Optional" on this line: [paste line]. Here's the function: [paste]. Explain why it's nil in plain terms and give me the safe version. Don't just add another force-unwrap.

The last sentence matters — without it, models sometimes 'fix' a crash by hiding it. Steer toward the honest fix.

Paste the log, not a paraphrase

Copy the actual console text. 'It crashed when I tapped save' gives the model nothing; the real error string plus the function usually gets a correct fix on the first try.