Print unescaped string output in Swift

⋅ 1 min read ⋅ Swift Debugging

Table of Contents

I use po (print object) a lot when debugging. In Swift, Xcode will escape special characters[1] like a single quote, double quotes, and newline character. This is fine most of the time since we just want to see the value of the object, but there is a time where we might need to use this print out value for further debugging; mine is when I want to debug a JSON response e.g., Validate format to find whether it is malformed or not.

{
"key": "value",
"key2": "value2",
"key3": "key3's value"
}

Here is how the above JSON looks like when I print it out:

(lldb) po string
"{\n \"key\": \"value\",\n \"key2\": \"value2\",\n \"key3\": \"key3\'s value\"\n}\n"

Solution

We can print out an unescaped string by call Swift print or wrapping it with NSString.

(lldb) po NSString(string: string)
{
"key": "value",
"key2": "value2",
"key3": "key3's value"
}

or

(lldb) po print(string)
{
"key": "value",
"key2": "value2",
"key3": "key3's value"
}

  1. https://docs.swift.org/swift-book/LanguageGuide/StringsAndCharacters.html ↩︎


Read more article about Swift, Debugging, or see all available topic

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Tweet Share
Previous
Sign in with Apple Tutorial, Part 3: Backend – Token verification

Part 3 in a series Sign in with Apple. In this part, we will see how backend can use the token to sign up/sign in users.

Next
Sign in with Apple Tutorial, Part 4: Web and Other Platforms

Part forth in a series Sign in with Apple. Use Sign in with Apple JS to let users set up accounts and sign in to your website and apps on other platforms.

← Home