Last active
August 15, 2020 00:45
-
-
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
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 | |
| 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