iOS Debugging Tips
— Swift, Debugging — 2 min read
Collection of some iOS Debugging tips.
1. Changing variable values
Add a breakpoint where you want to change the variable value. Then use the following command (lldb) to update the variable's value.
In this example, I am updating the value of address
variable.
expression address = "New address"
You can use expr
or ex
(short forms) instead of expression
.
2. Printing variable value
When the execution stops at a breakpoint, you can use one of the following to print the value of a variable to the console:
-
p
(a.k.aexpr --
) - This prints the value of the given variable. Usage -p address
-
po
(a.k.aexpr -O --
) - This also works asp
, but if the passed variable is an object, it prints the output of thedescription
for that object. Usage -po address
3. Printing values of non-computed variables
Use v
instead of p
or po
to print the value of non-computed variables (e.g. IndexPath).
4. Skipping lines of code to execute (Moving the instruction pointer)
While debugging, you can move the instruction pointer to the desired location to start execution from that line.
Or, you can use this lldb command to jump by n number of lines:
thread jump --by 1
5. Printing view tree (recursive descriptor)
Using this lldb command to print the view hierarchy for a particular view:
expression -l objc -O -- [view recursiveDescription]
6. Adding aliases
To create an alias for an lldb command that you use often, you can create an alias like this:
command alias poc expression -l objc -O --
Now, instead of typing expression -l objc -O -- [view recursiveDescription]
, you can simply use poc [view recursiveDescription]
.
7. Printing object description using a memory address
Use this lldb command to print the description of a memory address.
expression -l objc -O -- 0x7fc2b240e3a0
Or using the alias we created earlier:
poc 0x7fc2b240e3a0
8. Returning early from a function
Use the following lldb command to return early:
thread return
9. Changing UIView properties on the fly
To change the property of a UIView, use this:
po unsafeBitCast(0x7fc2b240e3a0, to: UILabel.self).text = "New text"
Followed by:
expression CATransaction.flush()
10. Adding a fancy description for your own classes
To print a different output to the console when an instance of your classes is printed, you can use CustomDebugStringConvertible
.
Usage:
extension YourClass: CustomDebugStringConvertible { var debugDescription: String { "Your custom description" }}