Skip to content

Instantly share code, notes, and snippets.

@atierian
Last active November 30, 2020 00:48
Show Gist options
  • Select an option

  • Save atierian/6ef9ebd9721d621e139f803b40bd1d1d to your computer and use it in GitHub Desktop.

Select an option

Save atierian/6ef9ebd9721d621e139f803b40bd1d1d to your computer and use it in GitHub Desktop.
Extension on Data to pretty print in debug console
extension Data {
var prettyPrintedJSON: NSString? {
guard let object = try? JSONSerialization.jsonObject(with: self, options: []),
let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted, .withoutEscapingSlashes]),
let prettyPrintedString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else { return nil }
return prettyPrintedString
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment