// // ListCategoryView.swift // TabBarProject // // Created by Arthur Dambrine on 06/04/2021. // import SwiftUI struct Category: Codable, Identifiable { let id = UUID() let id_categorie: Int let nom: String } class apiCall { func getCategories(completion:@escaping ([Category]) -> ()) { guard let url = URL(string: "https://api.art-dambrine.ovh/categories") else { return } URLSession.shared.dataTask(with: url) { (data, _, _) in let categories = try! JSONDecoder().decode([Category].self, from: data!) print(categories) DispatchQueue.main.async { completion(categories) } } .resume() } } struct ListCategoryView: View { @State var categories: [Category] = [] var body: some View { VStack { Button { print("buttonClicked") apiCall().getCategories { (categories) in self.categories = categories } } label : { Text("Afficher les categories") } List(categories) { categorie in Text(String(categorie.id_categorie)) .font(.headline) Text(categorie.nom) .font(.subheadline) } } } } struct ListCategoryView_Previews: PreviewProvider { static var previews: some View { ListCategoryView() } }