Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing a multiline string in Xcode debugging console

I built a basic tree data structure for my Swift-based iOS app, and I wanted to be able to print it out to the console for debugging purposes. So I wrote a function that would traverse through the tree (preorder) and print off the value at each node on it's own line with a number of tabs inserted before it based on the node's depth in the tree. By calling the function somewhere in the code, in the debugging console it would print like this:

root
    node1
        node1_1
        node1_2
    node2
        node2_1
            node2_1_1
        node2_2
    node3
        node3_1

etc. This was good, and it allowed me to just call printInfo() in important places in the code, but I quickly realized that I needed to be able to print a String value on the spot like when I was paused at breakpoints. So I created a computed property infoString that would build a String from the tree with all the newlines (\n) and tabs (\t) thinking that I could just print it out in the debug console when I was at a breakpoint.

I tried it out with the command po infoString, but what it prints out to the console is this: "root\n\tnode1\n\t\tnode1_1\n\t\tnode1_2\n\tnode2\n\t\tnode2_1\n\t\t\tnode2_1_1\n\t\tnode2_2\n\tnode3\n\t\tnode3_1", which is technically correct, since that is what the String is, but I wanted to actually see the newlines and tabs.

So how do I print my String so that I see the newlines and tabs?

like image 488
Tim Fuqua Avatar asked Oct 14 '17 02:10

Tim Fuqua


People also ask

How do I debug in Xcode step by step?

When you run an application in Xcode, the debugger is automatically started and attached to the process of the application. Click the Run button in the top left or press Command + R. From the moment the application is up and running, we can start inspecting the process and, if necessary, debug it.


1 Answers

I spent a while digging around StackOverflow and investigated the differences between p (print) and po (print object), and certainly learned a lot, but I still couldn't find what I was looking for.

Then I recalled another time where I was debugging some password encoding code where I needed to manually replace certain characters with "escaped" versions of the character (so a double-quote " would be replaced with \", and a single-quote ' would be replaced with \'). When I went to test that I was replacing the characters properly, printing out the Strings would result in really convoluted and hard to read output strings. I don't remember how I stumbled upon it, but I converted the String to an NSString and printed it out that way.

Voila! It looked correct with the escaped characters showing up exactly as they should. So, having remembered that trick, I tried it out with my infoString var.

Sure enough, when I tried po infoString as NSString, it printed exactly like the function version did.

So that's the trick. Convert the String to an NSString when you print it out in the debug console.

If you have any other recommendations, feel free to add them.

like image 182
Tim Fuqua Avatar answered Dec 15 '22 21:12

Tim Fuqua