Swift version: 5.2
You’re probably already familiar with Xcode’s breakpoints, which allow you to pause code when a certain point is reached. Watchpoints also let you pause code, but their job is to pause when a variable is changed anywhere else in your code – it’s like a property observer, except implemented entirely using Xcode’s debugger.
To try it out, place a breakpoint in your program next to a variable you want to watch, then run your code. When the breakpoint hits, look for your variable inside the variables view – that’s in Xcode’s debug area at the bottom, to the left of the console.
When you find your variable, right-click on it and choose “Watch”. Once that’s done you can continue your program as normal, and anywhere the variable is read from or written to Xcode will pause and you can use the debug navigator to step through the call stack to figure out what happened.
SPONSORED Are you tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically, and it only takes a line of code to setup. Start your free trial now and get 3 months off exclusively for the Hacking with Swift Community.
Available from iOS
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.