I don't get any error when I fetch data but when I run I don't see any data
Thank you
Code Block import SwiftUI import Combine struct ContentView: View { @ObservedObject var networkController = NetworkController() @State var search: String = "" var body: some View { NavigationView{ Form{ Section(){ TextField("Search", text: $search) } Section(){ List(networkController.users.filter { $0.state.contains(search) } , id: \.state){ user in Text(user.state) } } } .navigationTitle("API") } } } class NetworkController: ObservableObject { private var can: AnyCancellable? let url = URL(string: "https://api.covidtracking.com/v1/states/current.json")! @Published var users = [User(state: "")] init() { self.can = URLSession.shared.dataTaskPublisher(for: url) .map { $0.data } .decode(type: [User].self, decoder: JSONDecoder()) .replaceError(with: []) .eraseToAnyPublisher() .receive(on: DispatchQueue.main) .sink(receiveValue: { users in self.users = users }) } } struct User: Decodable { var state: String }
Thanks for showing the result. The most important part is the value of completion at the bottom:
Code Block failure(Swift.DecodingError.valueNotFound(Swift.Int, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 3", intValue: 3), CodingKeys(stringValue: "deathConfirmed", intValue: nil)], debugDescription: "Expected Int value but found null instead.", underlyingError: nil)))
"Expected Int value but found null instead." means some value, which you expect as integer, was not an integer but null.
_JSONKey(stringValue: "Index 3", intValue: 3) represents that was in the fourth element of the JSON array.
CodingKeys(stringValue: "deathConfirmed", intValue: nil) states that the key was "deathConfirmed".
Code Block [ ... { "date": 20201227, "state": "AS", "positive": 0, "probableCases": null, "negative": 2140, "pending": null, "totalTestResultsSource": "totalTestsViral", "totalTestResults": 2140, "hospitalizedCurrently": null, "hospitalizedCumulative": null, "inIcuCurrently": null, "inIcuCumulative": null, "onVentilatorCurrently": null, "onVentilatorCumulative": null, "recovered": null, "dataQualityGrade": "D", "lastUpdateEt": "12/1/2020 00:00", "dateModified": "2020-12-01T00:00:00Z", "checkTimeEt": "11/30 19:00", "death": 0, "hospitalized": null, "dateChecked": "2020-12-01T00:00:00Z", "totalTestsViral": 2140, "positiveTestsViral": null, "negativeTestsViral": null, "positiveCasesViral": 0, "deathConfirmed": null, "deathProbable": null, "totalTestEncountersViral": null, "totalTestsPeopleViral": null, "totalTestsAntibody": null, "positiveTestsAntibody": null, "negativeTestsAntibody": null, "totalTestsPeopleAntibody": null, "positiveTestsPeopleAntibody": null, "negativeTestsPeopleAntibody": null, "totalTestsPeopleAntigen": null, "positiveTestsPeopleAntigen": null, "totalTestsAntigen": null, "positiveTestsAntigen": null, "fips": "60", "positiveIncrease": 0, "negativeIncrease": 0, "total": 2140, "totalTestResultsIncrease": 0, "posNeg": 2140, "deathIncrease": 0, "hospitalizedIncrease": 0, "hash": "2787ae39f1498a05878980d1f6195d5d78c19523", "commercialScore": 0, "negativeRegularScore": 0, "negativeScore": 0, "positiveScore": 0, "score": 0, "grade": "" }, ... ]
Can you see "deathConfirmed": null,? The API may return null for "deathConfirmed".
Your model type needs to be ready for null:
Code Block struct User: Decodable, Hashable { var state: String var positive: Int var deathConfirmed: Int? //<- }
Your DetailView needs to adapt to this change:
Code Block struct DetailView: View { var gridItemLayout = [GridItem(.flexible()), GridItem(.flexible())] var positive: Int var state: String var deathConfirmed: Int? //<- var body: some View { ... GroupBox{ VStack{ Text("State: ") //<-??? Divider() Text(deathConfirmed.map{"\($0)"} ?? "?") //<- .cornerRadius(16) } }.padding() } .navigationBarTitle(state) } } }