Debugging in Swift

Seasoned Objective-C developers have numerous tips and tricks that they use to aid the debugging process. However, some of these don’t seem as fruitful or are simply not available in Swift and working in the console can sometimes feel clunky and laborious.

For example, you can po a variable from the console but the output is sometimes not ideal. Consider an arbitrary dictionary:

Compare this with the same command on the same object in the Objective-C, which is better still from a readability perspective:

{
apples = 3;
bananas = 2;
oranges = 5;
pears = 1;
}

One way of getting around this is, instead of using po <variable name>, you can use po print(<variable name>). This gives us the kind of same pretty looking console output that we’re used to:

["oranges": 5, "pears": 1, "bananas": 2, "apples": 3]

However, typing this all the time from the console will likely cause finger injuries. Luckily, we can add this command to our .lldbinit file, which LLDB reads at startup. You will find it in the home folder on your Mac (if it’s not there just create one). To do this we need the command regex command:

command regex dp 's/(.+)/po print(%1)/'

The command regex command allows the user to create powerful regular expression commands (regex) with substitutions. The regex and substitutions are specified using the format: s/<regex>/<substitution(s)>/. Input can be captured using parentheses in the regex. This input is then accessible via %1 for the first capture group and %2 for the second, etc.

In this case I’ve named my custom po command dp so, now I can just type dp <variable name> to get this behaviour.

Inspecting the view hierarchy

I’ve become very fond of Chisel, a set of LLDB extension commands from Facebook. In particular there are some extremely useful commands that I use all the time in Objective-C like pviews <view variable name>, which prints the view hierarchy for a given view (or the entire view hierarchy when run without a view as an argument), e.g:

However, many users are finding these commands either misbehave or don’t work at all in a Swift context.

These two commands in particular access private APIs behind the scenes such as -[UIView recursiveDescription] and +[UIViewController _printHierarchy], which are not available when stopped in a Swift frame in the debugger.

In the Advanced Debugging In Swift WWDC 2014 session, they offered some clues as to how we might get around this by telling LLDB to use Objective-C when evaluating an expression: expr -l objc++ -O.

As you can imagine, -l objc++ forces the use of Objective-C, while the -O flag stands for 'object description'. We can now use a command like expr -l objc++ -O -- (id)<object pointer> to print an Objective-C style description of an object in the console (an alternative to the method outlined above). And, because we're telling LLDB to use Objective-C, we can use all the private methods we want.

Here I’ve called our custom pviews command spv. The only caveat is that we need to run a prior command at the console in order to get the pointer address to pass into spv.

As for pvc (see above), that doesn’t seem to work at all in Chisel 1.2.0, which is the latest version as of this writing. This is a shame because it can be a real time saver, particularly when starting on new codebases.

Previously you could simply run the app, hit pause on the debugger in Xcode and type pvc from the console and out pops a visual depiction of the current view controller hierarchy! Luckily we can easily add another entry in our .lldbinit file to make this work:

Note that we can use command alias as opposed to command regex, which basically allows us to do more complex commands using a ‘shortcut’ command; spvc in this case. Now doing spvc gets the same awesome output from a Swift frame.

Data Formatters

Swift uses data formatters to print a description of a variable in the console as opposed to Objective-C’s use of -[NSobject description], which is why we see the output we do when poing the contents of a dictionary in the console in Swift, for example. However, LLDB does allow you to add your own data formatters by using the type summary add command. So you can pretty up that data to your heart’s content. Here's a contrived example with an ‘address’ struct:

This binds the formatting inside the quotes ("") to the type at the end (TestApp.Address). Note that it’s necessary to include the module name prefix of the type when adding the data formatter. You can reference members of the variable with the special ${var.<member name>} syntax. Notice the selective interspersing of spacing, commas and new line characters to make this format more like a real address. Now when we do p address, we get:

The Advanced Debugging in Swift session discusses printing the output of these custom formatters using the po command. However, I needed to use the p command. Alternatively, you could use fr v (frame variable). Indeed the docs for type summary add suggest this is the way it needs to be.

The cool thing is that you can traverse relationships so you could print more complex descriptions. Let’s assume that recipient in the Address struct was itself another struct such as:

struct Recipient {
var name: String
}

Then the data formatter could be created by traversing the relationship with type summary add -s "\n${var.recipient.name},...

These data formatters will be available across runs of your application. However, because the version of LLDB that comes with Xcode doesn’t look in the working directory for an .lldbinit file, they would need to be added again after restarting Xcode. Of course, you could put these commands in your .lldbinit file but that isn’t really the place for project specific commands.

Alternatively, you could create a separate file (.lldbcommands, for example), which adds your data formatters (or any other project specific LLDB commands), which you then load using the command source <file path> LLDB command. Simply add a breakpoint that auto-continues in applicationDidFinishLaunching() that runs this command and you’ll have all your favourite data formatters ready to go.

Otherwise, you could check out this interesting looking Xcode plugin, which seems to solve this very problem (not tested). If you're really eager you can even configure this stuff using Python scripts. Check out the docs with help type summary add.

Swizzling

Swizzling can be a very useful method of debugging in situations where the code you want to inspect is not reachable because you want to check something on a class that is owned by the frameworks or is too deep down in your own framework, for example.

Want to see what’s happening when addSubview() gets called on views that are owned by framework classes? Swizzling that method on UIView may be able to help. What I think some people don’t realise is that swizzling is not just an Objective-C luxury.

Every Swift app runs inside the Objective-C runtime, making swizzling a possibility since it’s all runtime methods that enable the swizzling magic to happen. Nate Cook has a really interesting article on how to achieve this including some tips and caveats. For example, NSObject.load(), a favourite place for developers to set up method swizzling, doesn’t get called in Swift so, it’s necessary to utilise NSObject.initialize(). But swizzling is still possible on pure Swift objects by simply decorating methods with the dynamic modifier.

Swift REPL (Read Eval Print Loop)

Lastly, the Xcode command line tools come with the Swift REPL, which is a way to interactively test your code, direct from the terminal. You can experiment with the syntax, define variables, classes and any other Swift type, run your code and view its output which can be handy for getting a quick view for how something might work. However, in practice, I suspect Playgrounds would be the preferred choice.