Logo for tanaschita.com

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.

Sponsorship logo
Preparing for a technical iOS job interview
Check out my new book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, Authentication, Core Data, Concurrency with async/await, Security, Automated Testing and more.
LEARN MORE

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"

Since we'd like to test the app with a different conservationStatus, we use the p command to change it:

(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.

Changing a variable by using breakpoint actions.
Changing a variable by using breakpoint actions.

The Name and Condition fields can stay empty. What we want is to configure a Debugger Command by adding an action with the + button. Here, we add the same command we used in the console.

Additionally, checking the Automatically continue after evaluating actions option ensures that the app doesn't stop at the breakpoint after executing the command.

Conclusion

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.

Sponsorship logo
Preparing for a technical iOS job interview
Check out my new book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, Authentication, Core Data, Concurrency with async/await, Security, Automated Testing and more.
LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

debugging

xcode

ios

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

Latest articles and tips

© 2023 tanaschita.com

Privacy policy

Impressum