Categories
Swift Xcode

Basic debugging in Xcode

Printing object description

Probably the most used command when debugging apps is po. It evaluates the expression and prints out the object description. When printing custom objects, it should be noted that the format of the result can be customised by implementing debugDescription in CustomDebugStringConvertible protocol.

Modifying code in runtime

LLDB’s expression command allows executing code when hitting a breakpoint. It is very useful for changing internal state of the app for reproducing hard to catch issues. For example, in a game there is a button combo or custom gesture what triggers a specific code path. Reproducing it can be time consuming and alternatively we can change the state of the app to always trigger it (e.g. calling a function when tapping on the screen). Another use-case is rapid prototyping. When fine-tuning animations, we can change values of the animation using a breakpoint and then triggering it from the app. Time saved by not rerunning the app after changing one value. In the example above, we are changing CAEmitterCell’s velocity property from 80 to 160.

Symbolic breakpoints

Symbolic breakpoints are helpful when stopping execution when specific function is called. Symbolic breakpoints can be added by opening breakpoint navigator in Xcode -> add button -> Symbolic breakpoint. Example above sets breakpoint to Objective-C method as QuartzCore is written in Objective-C. Example below illustrates symbolic breakpoint of a Swift function. When Objective-C symbolic breakpoint stops the execution, we can use $arg1 for printing out self.

Conditional breakpoint

Another very useful feature is setting conditional breakpoints. We can just write boolean expression to the condition field and LLDB will stop execution when it evaluates to true. Very useful when dealing with loops and trying to catch a specific case.

Summary

I would consider mentioned tips as the basic knowledge when debugging apps in Xcode. Many of those techniques can help to save tremendous amount of time as we do not need to rerun the app so often and we can skip reproducing the error condition in the app manually.

If this was helpful, please let me know on Mastodon@toomasvahter or Twitter @toomasvahter. Feel free to subscribe to RSS feed. Thank you for reading.

Resources