Skip to content

Instantly share code, notes, and snippets.

@ryantxr
Created October 11, 2017 17:36
Show Gist options
  • Save ryantxr/9e40ee7c7416a1b0732e29d70630b374 to your computer and use it in GitHub Desktop.
Save ryantxr/9e40ee7c7416a1b0732e29d70630b374 to your computer and use it in GitHub Desktop.
JSON output from API Call
/*
JSON from API calls are provided as Data objects, not strings.
While debugging, it is often necessary to output the JSON
string somewhere. This snippet does that.
*/
if let data = data {
print("JSON = " + String(data: data, encoding: .utf8)! )
}
// Notice the use of ! since the String constructor might fail.
// Here is an alternative if you want to avoid forced unwrap
if let data = data {
print("JSON = " + String(data: data, encoding: .utf8) ?? "Error converting to string" )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment