Skip to content

Instantly share code, notes, and snippets.

@rahulsingh1101
Created August 26, 2021 08:06
Show Gist options
  • Save rahulsingh1101/64347099f3c867420725bff9bbe48571 to your computer and use it in GitHub Desktop.
Save rahulsingh1101/64347099f3c867420725bff9bbe48571 to your computer and use it in GitHub Desktop.
extension Dictionary {
var prettyPrintedJSON: String? {
do {
let data: Data = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
return String(data: data, encoding: .utf8)
} catch _ {
return nil
}
}
var data: Data? {
do {
let jsonData = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
return jsonData
} catch {
print("dictionary to data conversion error : \(error.localizedDescription)")
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment