Last active
April 17, 2020 00:44
-
-
Save navsing/c45a8a3b389a01bedb2d7d5bbcbc98f4 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 SwiftUI | |
| struct ContentView: View { | |
| @State var results = [TaskEntry]() | |
| var body: some View { | |
| List(results, id: \.id) { item in | |
| VStack(alignment: .leading) { | |
| Text(item.title) | |
| } | |
| }.onAppear(perform: loadData) | |
| } | |
| func loadData() { | |
| guard let url = URL(string: "https://jsonplaceholder.typicode.com/todos") else { | |
| print("Your API end point is Invalid") | |
| return | |
| } | |
| let request = URLRequest(url: url) | |
| URLSession.shared.dataTask(with: request) { data, response, error in | |
| if let data = data { | |
| if let response = try? JSONDecoder().decode([TaskEntry].self, from: data) { | |
| DispatchQueue.main.async { | |
| self.results = response | |
| } | |
| return | |
| } | |
| } | |
| }.resume() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment