Hello
I'm trying to fetch some data from a JSON Api, but when I access the properties of my struct it tells me Value of Type "Name Of The Struct" has no member "name of the property"
Any idea on how to fix it
Code:
Thank You
I'm trying to fetch some data from a JSON Api, but when I access the properties of my struct it tells me Value of Type "Name Of The Struct" has no member "name of the property"
Any idea on how to fix it
Code:
Code Block import SwiftUI import Combine struct ContentView: View { @ObservedObject var networkController = NetworkControllerItalia() var gridItemLayout = [GridItem(.flexible()), GridItem(.flexible())] var body: some View { NavigationView{ ScrollView{ ForEach(networkController.users, id: \.self){ user in GroupBox(label: Text(user.round ?? ""), content: { VStack{ Text(user.team1 ?? "") } }).padding() } .navigationTitle("Serie A") } } } } class NetworkControllerItalia: ObservableObject { private var can: AnyCancellable? let url = URL(string: "https://raw.githubusercontent.com/openfootball/football.json/master/2020-21/it.1.json")! //let url = URL(string: "google.com")! @Published var users = [UserItalia(matches: [])] init() { self.can = URLSession.shared.dataTaskPublisher(for: url) .map { $0.data } .decode(type: [UserItalia].self, decoder: JSONDecoder()) //.replaceError(with: []) .eraseToAnyPublisher() .receive(on: DispatchQueue.main) .sink(receiveCompletion: {completion in print(completion) }, receiveValue: { users in self.users = users }) } } struct UserItalia: Decodable, Hashable { var matches: [Matches]? } struct Matches: Decodable, Hashable { var round: String? var date: String? var team1: String? var team2: String? }
Thank You
Of course, UserItalia is not an array.'ForEach' requires that 'UserItalia' conform to 'RandomAccessCollection'
You need to understand better what your data structures are.
networkController.users is UserItalia type.
What you want to access are matches, isn't it ?
So, you should write something like this
Code Block ForEach(networkController.users.matches ?? [], id: \.self){ match in GroupBox(label: Text(match.round ?? ""), content: { VStack{ Text(match.team1 ?? "") } }).padding()
I tested and it works (iOS 14.2).
If that's OK, don't forget to close the thread on this answer, otherwise please detail the exact error you get.