Print unescaped string output in Swift

Swift Debugging

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 ↩︎

If you enjoy this article, you can subscribe to the weekly newsletter.

Every Friday, you’ll get a quick recap of all articles and tips posted on this site — entirely for free.


← Home