I am trying to show a list of teams in a list with a ForEach loop but the text does not appear. Here is my code:
struct NFL: Decodable, Identifiable {
var id: String?
var status: Int?
var time: String?
var teams: Int
var results: [TeamResultNFL]?
}
struct TeamResultNFL: Decodable, Identifiable, Hashable {
var id: String?
var team: String?
var mascot: String?
var location: String?
var conference: String?
var division: String?
var league: String?
var abbreviation: String?
}
struct LaunchView: View {
var result: [TeamResultNFL]
var body: some View {
List {
ScrollView {
ForEach(result, id: \.self) { game in
Text(game.team ?? "HELLO")
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
LaunchView(result: [TeamResultNFL]())
}
}
@Published var nflResult = [TeamResultNFL] ()
But nflResult is nowhere populated.
do {
let decoder = JSONDecoder()
let result = try decoder.decode(NFL.self, from: data!)
}
You should populate it here:
do {
let decoder = JSONDecoder()
let result = try decoder.decode(NFL.self, from: data!)
nflResult = result.results
}