Problems with ForEach, SwiftUI

Hello
I am getting a lot of errors in ForEach and I don't know why at all.
Here is the code:
The error is at line 12
Code Block
import SwiftUI
import Combine
struct ContentView: View {
@EnvironmentObject var networkController: NetworkControllerItalia
var body: some View {
Form{
TextField("Input city name", text: $networkController.cityName)
Section {
ForEach(networkController.users.weather, id: \.self){ user in
}
}
}
}
}
class NetworkControllerItalia: ObservableObject {
private var can: AnyCancellable?
@Published var cityName: String = ""
@Published var users = [UserItalia(weather: Weather())]
init(cityName: String) {
self.cityName = cityName
let url = URL(string: "http://api.openweathermap.org/data/2.5/weather?q=\(cityName)&appid=")!
self.can = URLSession.shared.dataTaskPublisher(for: url)
.map { $0.data }
.decode(type: [UserItalia].self, decoder: JSONDecoder())
.eraseToAnyPublisher()
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: {completion in
print(completion)
}, receiveValue: { users in
self.users = users
})
}
}
struct UserItalia: Decodable, Hashable{
var weather: Weather
}
struct Weather: Decodable, Hashable {
var main: String?
}

Thank you
Answered by Claude31 in 669883022
I would change code with:
Code Block
                ForEach(networkController.users, id: \.self){ user in

and use user.weather later in the closure (you did not show what you do there, so hard to say more).
Errors:
Cannot infer key path type from context; consider explicitly specifying a root type
Insert '<#Root#>'

Extra arguments at positions #2, #3 in call

Generic parameter 'Content' could not be inferred
Explicitly specify the generic arguments to fix this issue

Missing argument for parameter 'content' in call
Insert ', content: <#(Int) -> _#>'

Value of type '[UserItalia]' has no member 'weather
Accepted Answer
I would change code with:
Code Block
                ForEach(networkController.users, id: \.self){ user in

and use user.weather later in the closure (you did not show what you do there, so hard to say more).
Actually this is not working:

ForEach(networkController.users, id: \.self){ user in

I thought it was right, that's why I checked it
Problems with ForEach, SwiftUI
 
 
Q