ForEach won't show text

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]())
    }
}
Answered by Claude31 in 703735022
    @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
                        }

You do not initialise result: it is empty. So ForEach has nothing to display.

Change in SceneDelegate

        let contentView = LaunchView(result: [TeamResultNFL(id: "id", team: "team", mascot: "mascot", location: "loc", conference: "conference", division: "div", league: "league", abbreviation: "abbr"), TeamResultNFL(id: "id2", team: "team2", mascot: "mascot", location: "loc", conference: "conference", division: "div", league: "league", abbreviation: "abbr")]) 

        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }

Do the same in Previews.

struct LaunchView_Previews: PreviewProvider {

    static var previews: some View {

        LaunchView(result: [TeamResultNFL(id: "id", team: "team", mascot: "mascot", location: "loc", conference: "conference", division: "div", league: "league", abbreviation: "abbr"), TeamResultNFL(id: "id2", team: "team2", mascot: "mascot", location: "loc", conference: "conference", division: "div", league: "league", abbreviation: "abbr")])
    }

}

Here is formatted:

func getNFLTeams() {
    let headers = [
        "x-rapidapi-host": "sportspage-feeds.p.rapidapi.com",  "x-rapidapi-key": ""]
    let urlString = "https://sportspage-feeds.p.rapidapi.com/teams?league=NFL"
    let url = URL(string: urlString)
    if url != nil {
        var request = URLRequest(url: url!, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 10.0)
        request.httpMethod = "GET"
        request.allHTTPHeaderFields = headers
        let session = URLSession.shared
        let dataTask = session.dataTask(with: request) {data, response, error in
            if error == nil {
                do {
                    let decoder = JSONDecoder()
                    let result = try decoder.decode(NFL.self, from: data!)
                    print(result)
                }
                catch { print(error)  }
                
            }
            
        }
        dataTask.resume()
    }
    
}

this is not working

That does not give any information.

  • What do you get (crash ? empty list ?)
  • You get a result, but it disappears as soon as func ends.
  • Probably, getNFLTeams should return the result:
func getNFLTeams() -> [TeamResultNFL]
  • Where is this func ?

  • Does it return a single NFL or the whole array [TeamResultNFL] ?

  • What do you get in print(result) ?

  • if it has to be all results, the, decode is wrong. Should use

  • Where and how do you use getNFLTeams() ?

                    let result = try decoder.decode([TeamResultNFL].self, from: data!)    // But that's just guessing

Probably you should use it in scene delegate:

        let results = getNFLTeams()     // If getNFLTeams() effectively returns all the results
        let contentView = LaunchView(result: results) 

So please, when you report problem, give enough information and show the whole code needed to understand.

I apologize I am new at this. The getNFLTeams func is in my ContentModel. In the print console I get the results from the struct NFL and TeamResultNFL but I am not able to show the results in the view it just shows an empty list.


class ContentModel: ObservableObject {

    @Published var nfl = [NFL]()
    @Published var nflResult = [TeamResultNFL]()
    
init() {
         getNFLTeams()
    }

        func getNFLTeams() {

            let headers = ["x-rapidapi-host": "sportspage-feeds.p.rapidapi.com", "x-rapidapi-key": " ]
            let urlString = "https://sportspage-feeds.p.rapidapi.com/teams?league=NFL"
            let url = URL(string: urlString)
            if url != nil {
                var request = URLRequest(url: url!, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 10.0)
                request.httpMethod = "GET"
                request.allHTTPHeaderFields = headers
                let session = URLSession.shared
                let dataTask = session.dataTask(with: request) { data, response, error in
                    if error == nil {
                        do {
                            let decoder = JSONDecoder()
                            let result = try decoder.decode(NFL.self, from: data!)
                            print(result)
                        }
                        catch {
                            print(error)
                        }
                    }
                }
                dataTask.resume()
            }
        }
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]

    @EnvironmentObject var model: ContentModel

    var body: some View {

          List {
            
              ScrollView {

                    ForEach(result) { game in
                        Text(game.team ?? "")
                }
             }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        LaunchView(result: [TeamResultNFL(id: "", team: "", mascot: "", location: "", conference: "", division: "", league: "", abbreviation: "")])
            .environmentObject(ContentModel())
    }
}
import SwiftUI

@main

struct SportsBetsApp: App {

    var body: some Scene {

        WindowGroup {

            LaunchView(result: [TeamResultNFL]())
                .environmentObject(ContentModel())
            }
        }
    }

Here is all the code again I apologize and thank you for any input @Claude31

Accepted Answer
    @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
                        }
ForEach won't show text
 
 
Q