Skip to content

Instantly share code, notes, and snippets.

@Edudjr
Last active April 7, 2022 17:14
Show Gist options
  • Save Edudjr/e8464ccad4f8fa94afdfed84bb7d4882 to your computer and use it in GitHub Desktop.
Save Edudjr/e8464ccad4f8fa94afdfed84bb7d4882 to your computer and use it in GitHub Desktop.
A Swift 3 extension for pretty-printing JSON (in swift: [String: Any])
// Dictionary/JSON Extension
// Pretty Print Json Objects
//
// Created by Domene on 10/02/17.
//
import Foundation
extension Dictionary where Key == String, Value == AnyObject {
func prettyPrint() -> String{
var string: String = ""
if let data = try? JSONSerialization.data(withJSONObject: self, options: .prettyPrinted){
if let nstr = NSString(data: data, encoding: String.Encoding.utf8.rawValue){
string = nstr as String
}
}
return string
}
}
//Usage: print(json.prettyPrint())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment