Skip to content

Instantly share code, notes, and snippets.

@akshitzaveri
Created May 25, 2020 07:20
Show Gist options
  • Save akshitzaveri/2bfc600fa8b62f5263b4c6b3d31e3fbb to your computer and use it in GitHub Desktop.
Save akshitzaveri/2bfc600fa8b62f5263b4c6b3d31e3fbb to your computer and use it in GitHub Desktop.
let url = URL(string: "http://jsonplaceholder.typicode.com/posts")!
let task = URLSession.shared.dataTask(with: url, completionHandler: { data, response, error in
guard let data = data else { return completion([]) }
do {
let posts = try JSONDecoder().decode([Post].self, from: data)
completion(posts)
} catch {
print(error)
completion([])
}
})
task.resume()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment