Last active
April 7, 2022 17:14
-
-
Save Edudjr/e8464ccad4f8fa94afdfed84bb7d4882 to your computer and use it in GitHub Desktop.
A Swift 3 extension for pretty-printing JSON (in swift: [String: Any])
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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