Created
January 29, 2018 20:21
-
-
Save stevenojo/16420072be4b2fd95742a774b9c58805 to your computer and use it in GitHub Desktop.
Convert Dictionary to a JSON string in swift
This file contains hidden or 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 Dictionary { | |
func jsonString() -> NSString? { | |
let jsonData = try? JSONSerialization.data(withJSONObject: self, options: []) | |
guard jsonData != nil else {return nil} | |
let jsonString = String(data: jsonData!, encoding: .utf8) | |
guard jsonString != nil else {return nil} | |
return jsonString! as NSString | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment