Created
August 17, 2020 02:12
-
-
Save simrandotdev/46da2a6a8ad7e909487cb4d1a6490c4c 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
| import UIKit | |
| func loadData() { | |
| guard let url = URL(string: "https://reqres.in/api/users") else { return } | |
| URLSession.shared.dataTask(with: url) { (data, response, error) in | |
| if let error = error { | |
| print("Something went wrong with the request: \(error.localizedDescription)") | |
| return | |
| } | |
| guard let data = data else { | |
| print("No data found.") | |
| return | |
| } | |
| print(data) | |
| }.resume() | |
| } | |
| loadData() |
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
| import UIKit | |
| func loadData() { | |
| guard let url = URL(string: "https://reqres.in/api/users") else { return } | |
| URLSession.shared.dataTask(with: url) { (data, response, error) in | |
| if let error = error { | |
| print("Something went wrong with the request: \(error.localizedDescription)") | |
| return | |
| } | |
| guard let data = data else { | |
| print("No data found.") | |
| return | |
| } | |
| do { | |
| let decoder = JSONDecoder() | |
| let result = try decoder.decode(Response.self, from: data) | |
| print(result) | |
| } catch let error { | |
| print("Cannot convert the response to the required objects: \(error.localizedDescription)") | |
| } | |
| }.resume() | |
| } | |
| struct Response : Decodable { | |
| var data: [User] | |
| } | |
| struct User: Decodable { | |
| let id: Int | |
| let email: String | |
| let first_name: String | |
| let last_name: String | |
| let avatar: String | |
| } | |
| loadData() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment