Hi guys! Today I'm quite confused about how to approach this problem that I'm going to explain you. It would be great if you could help me.
First, I just accomplished successfully the Apple tutorial to create a dynamic list from a local JSON file. But know, I need it from a JSON web server response. I'm receiving the data correctly into my app (I can print it and use it) but I don't know how to create a dynamic list with it. Let's write some code.
Example data (Response)
Data Model
Function request
As usual, then I'm calling the function from another structure
As I said, everything is working, the thing is I don't know how to create a list. I would like to make it in the following way:
1) Create a structure called FinancialRow -> design the row
2) Create a list in the main view
Thank you
Dennis
First, I just accomplished successfully the Apple tutorial to create a dynamic list from a local JSON file. But know, I need it from a JSON web server response. I'm receiving the data correctly into my app (I can print it and use it) but I don't know how to create a dynamic list with it. Let's write some code.
Example data (Response)
Code Block JSON {"results":[{"tag":"AccountsPayableCurrent","valueNum":266099000,"valueId":"USD","endtime":"20131231"}, {"tag":"AccountsPayableRelatedPartiesCurrent","valueNum":38000,"valueId":"USD","endtime":"20131231"}, {"tag":"AccountsReceivableNetCurrent","valueNum":243930000,"valueId":"USD","endtime":"20131231"}, {"tag":"AccruedLiabilitiesCurrent","valueNum":5583000,"valueId":"USD","endtime":"20131231"}, {"tag":"AccumulatedDepreciationDepletionAndAmortizationPropertyPlantAndEquipment","valueNum":120568000,"valueId":"USD","endtime":"20131231"}]}
Data Model
Code Block Swift import SwiftUI struct CompanyFinancialReportResponse: Codable { var results: [CompanyFinancialReportResult] } struct CompanyFinancialReportResult: Codable { var tag: String var valueNum: Float var valueId: String var endtime: String }
Function request
Code Block Swift import SwiftUI class CompanyFinancialReportFuction: ObservableObject { // Result variable. I have to make it with @Published @Published var companyFinancialReportResult = [CompanyFinancialReportResult]() @Published var financialReportShowResult: Bool = false // Define function API Request func financialReportRequest(url: String) { // First replace spaces for & in the parameter (searchText) let urlEncode = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? url guard let url = URL(string: urlEncode) else { print("Invalid URL") return } let request = URLRequest(url: url) URLSession.shared.dataTask(with: request) { data, reponse, error in if let data = data { if let decodedResponse = try? JSONDecoder().decode(CompanyFinancialReportResponse.self, from: data) { DispatchQueue.main.async { func getValues () -> Bool { self.companyFinancialReportResult = decodedResponse.results return true } if getValues() { self.financialReportShowResult = true } } return } } print("Fetch failed: \(error?.localizedDescription ?? "Unknown error")") }.resume() } }
As usual, then I'm calling the function from another structure
Code Block .onAppear
and receiving the data. As I said, everything is working, the thing is I don't know how to create a list. I would like to make it in the following way:
1) Create a structure called FinancialRow -> design the row
2) Create a list in the main view
Thank you
Dennis
I've got a coffee and know I see things clear. I just have to make the data
Code Block Hashable
Code Block swift import SwiftUI struct CompanyFinancialReportResponse: Codable { var results: [CompanyFinancialReportResult] } struct CompanyFinancialReportResult: Hashable, Codable { var tag: String var valueNum: Float var valueId: String var endtime: String }