Skip to content

Instantly share code, notes, and snippets.

@navsing
Last active April 17, 2020 00:44
Show Gist options
  • Save navsing/c45a8a3b389a01bedb2d7d5bbcbc98f4 to your computer and use it in GitHub Desktop.
Save navsing/c45a8a3b389a01bedb2d7d5bbcbc98f4 to your computer and use it in GitHub Desktop.
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