Created
May 25, 2020 07:20
-
-
Save akshitzaveri/2bfc600fa8b62f5263b4c6b3d31e3fbb to your computer and use it in GitHub Desktop.
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
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