Skip to content

Instantly share code, notes, and snippets.

@sourleangchhean168
Last active February 21, 2020 03:45
Show Gist options
  • Select an option

  • Save sourleangchhean168/f1a663c8524936af35221f410b588677 to your computer and use it in GitHub Desktop.

Select an option

Save sourleangchhean168/f1a663c8524936af35221f410b588677 to your computer and use it in GitHub Desktop.

Revisions

  1. sourleangchhean168 revised this gist Feb 21, 2020. No changes.
  2. sourleangchhean168 revised this gist Feb 21, 2020. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions dataPrettyPrintJSONString.swift
    Original file line number Diff line number Diff line change
    @@ -13,3 +13,10 @@ extension Data {
    let data = Data()
    print("data \(data!.prettyPrintedJSONString)")

    // {
    // "userId": 1,
    // "id": 1,
    // "title": "delectus aut autem",
    // "completed": false
    // }

  3. sourleangchhean168 created this gist Feb 21, 2020.
    15 changes: 15 additions & 0 deletions dataPrettyPrintJSONString.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    import Foundation

    extension Data {
    var prettyPrintedJSONString: NSString? { /// NSString gives us a nice sanitized debugDescription
    guard let object = try? JSONSerialization.jsonObject(with: self, options: []),
    let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]),
    let prettyPrintedString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else { return nil }

    return prettyPrintedString
    }
    }
    //Mark: Using
    let data = Data()
    print("data \(data!.prettyPrintedJSONString)")