2.6 C
New York
Thursday, December 5, 2024

ios – Xcode NavigationList ForEach loop from information mannequin


I’m having problem with looping via my API name decoded JSON information inside a navigation record. I’ve efficiently put the information right into a variable and simply can not seem to work out tips on how to loop via the information and record in Navigation view. The error I obtain is: “Generic struct ‘ForEach’ requires that ‘CompetitionGroup’ conform to ‘RandomAccessCollection'”

struct ContentView: View {
    let compse = competitionslist
    
    var physique: some View {
        NavigationView {
            Record {
                //ERROR ForEach(compse!) { competitors in
                    NavigationLink(vacation spot: Textual content(competitors.genericname)) {
                        Picture(systemName: "airplane")
                        Textual content(competitors.genericname)
                    }.padding()
                }
                .navigationTitle("Locations")
            }
        }
    }
}

The info is decoded as present under:

func  getComps() -> CompetitionGroup? {
    let headers = [
        "x-rapidapi-key": "apices",
        "x-rapidapi-host": "api.webnsite"
    ]

    let request = NSMutableURLRequest(url: NSURL(string: "https://football-web-pages1.p.rapidapi.com/competitions.json?embody=rounds")! as URL,
                                            cachePolicy: .useProtocolCachePolicy,
                                        timeoutInterval: 10.0)
    request.httpMethod = "GET"
    request.allHTTPHeaderFields = headers

    let session = URLSession.shared
    let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (information, response, error) -> Void in
        if (error != nil) {
            print(error as Any)
        } else {
            let httpResponse = response as? HTTPURLResponse
            
            do {
                let decoder = JSONDecoder()
                let comps = strive decoder.decode(CompetitionGroup.self, from: information!)
                self.competitions = comps
            }
            catch {
                print("Error in JSON")
            }
        }
    })

    dataTask.resume()
    return competitions
}

The CompetitionGroup mannequin holds an inventory of Competitions mannequin, each fashions structs are proven under. any assist can be appreciated.

public struct CompetitionGroup: Codable {
   var competitions:[Competition]?
}

public struct Competitors: Codable {
var id: Int?
var kind: String?
var genericname: String?
var fullname: String?

enum CodingKeys: String, CodingKey {
        case genericname = "generic-name"
        case fullname = "full-name"
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles