Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save simrandotdev/46da2a6a8ad7e909487cb4d1a6490c4c to your computer and use it in GitHub Desktop.

Select an option

Save simrandotdev/46da2a6a8ad7e909487cb4d1a6490c4c to your computer and use it in GitHub Desktop.
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()
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