How to change a variable's state while debugging an iOS application
Learn to use the expression LLDB command to change application state while debugging.
18 Jul 2022 · 3 min read
In the article on LLDB print commands for iOS debugging with Xcode, we looked at different LLDB print commands to evaluate variables in Xcode's console.
The expression LLDB command alias expr or p provides another interesting possibility. It allows us to change a variable's value while debugging on the fly without having to restart the iOS application.
This approach has the advantage that we don't need to change any code, for example when trying to fix a bug that needs a certain state to reproduce.
Let's see how this works.
Changing a variable in Xcode's console
When our app pauses at a breakpoint, we can use Xcode's console to change a variable to a state we need.
Let's imagine, we are trying to fix a bug that only is reproducable for birds that have a critically endangered conservation state. When a breakpoint hits the relevant line, let's at first print the bird's conservationStatus in Xcode's console:
(lldb) po bird.conservationStatus.title"endangered"
(lldb) p bird.conservationStatus = .cr
When printing the conservationStatus again, we get:
(lldb) po bird.conservationStatus.title"critically endangered"
By hitting the continue programm execution button, we are now able to test the application with a the new changed state.
Changing a variable by using breakpoint actions
When reproducing a bug, we sometimes want a variable to always have a certain value without typing the command every time in the console.
In this case, we can use a breakpoint action. After setting a breakpoint, right-click on it and select Edit Breakpoint.
Additionally, checking the Automatically continue after evaluating actions option ensures that the app doesn't stop at the breakpoint after executing the command.
The expression LLDB command provides a great possibility when debugging apps, the approach has its limits though, for example when working with constants. Trying to assign a new value to a let variable will return an error:
error: cannot assign to property:conservationStatus is a let constant
But besides from that and especially in more complex projects, the expression LLDB command can be a real time safer helping us to test our code or to reproduce a bug without having to restart the application.
Like to support my work?
Articles with related topics
Understanding LLDB print commands for iOS debugging with Xcode
Learn the difference between po, p, and v to inspect variables in Xcode's console.
11 Jul 2022 · 4 min read
Debugging iOS features that require restarting the app in Xcode
Quick tip on how to debug features like deep linking or home screen quick actions.
11 Apr 2022 · 2 min read
Get started with StoreKit Testing in Xcode
Learn how to test in-app purchases locally.
04 Sep 2023 · 4 min read
How to automate taking screenshots with fastlane for iOS
Learn how to setup fastlane's snapshot action.
24 Jul 2023 · 2 min read
Quick tip on how to unit test Swift optionals with XCTUnwrap
Learn how to shorten if let and guard statements when unit testing.
06 Mar 2023 · 2 min read
Latest articles and tips
Introduction to Kotlin for Swift developers - protocols, extensions & generics
Learn how protocols, extensions and generics in Kotlin work from a Swift developer's point of view.
02 Dec 2023 · 8 min read
How to store images in SwiftData
Learn how to efficiently store larger amounts of data in SwiftData.
27 Nov 2023 · 1 min read
How to migrate to a new schema with SwiftData in iOS
Learn how to create a migration plan in SwiftData.
20 Nov 2023 · 4 min read
How to implemet a free trial period for StoreKit 2 subscriptions in iOS
Learn how to implement StoreKit 2 introductory offers.
13 Nov 2023 · 2 min read
How to migrate from Localizable.strings to String Catalogs
Learn how to migrate your iOS project to .xcstrings for localization.
06 Nov 2023 · 1 min read