Last active
November 7, 2016 13:58
-
-
Save amosavian/531ee019717872ff0d283a750a782f32 to your computer and use it in GitHub Desktop.
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
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