Skip to content

Instantly share code, notes, and snippets.

@amosavian
Last active November 7, 2016 13:58
Show Gist options
  • Save amosavian/531ee019717872ff0d283a750a782f32 to your computer and use it in GitHub Desktop.
Save amosavian/531ee019717872ff0d283a750a782f32 to your computer and use it in GitHub Desktop.
extension NSDictionary {
convenience init ? (json: String) {
if let data = (try? JSONSerialization.jsonObject(with: json.data(using: String.Encoding.utf8, allowLossyConversion: true)!, options: .mutableContainers)) as? NSDictionary {
self.init(dictionary: data)
} else {
self.init()
return nil
}
}
func formatJSON() -> String? {
if let jsonData = try? JSONSerialization.data(withJSONObject: self, options: []) {
let jsonStr = String(data: jsonData, encoding: .utf8)
return jsonStr
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment