Skip to content

Instantly share code, notes, and snippets.

@simrandotdev
Last active August 15, 2020 00:45
Show Gist options
  • Select an option

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

Select an option

Save simrandotdev/dfd597cbc72f08bc587893a0ac0265ea to your computer and use it in GitHub Desktop.
Old Method Code for video transformTo from a data object to Codable Models
import UIKit
let url = URL(string: "https://reqres.in/api/users/2")!
URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print("Request failed with error: \(error?.localizedDescription)")
return
}
guard let data = data else {
print("Request failed: No Data found")
return
}
do {
let decoder = JSONDecoder()
let response = try decoder.decode(Response.self, from: data)
print(response)
} catch let error {
print("Failed to convert JSON response: \(error.localizedDescription)")
}
}.resume()
struct Response: Codable {
var data: User
}
struct User: Codable {
var id: Int
var email: String
var avatar: String
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment