RapidApi in xcode

Hi I am struggling with getting football data api from rapidapi to work in swift ui . here is the code below


The errors is get are "self.team = decodedTeams" Cannot find 'self' in scope"


and in my content view i get for " $network.getTeams"

Value of type 'EnvironmentObject<Network>.Wrapper' has no dynamic member 'getTeams' using key path from root type 'Network'


I have set out what i have in 2 pages of my swiftui code below

any help would be appreciated, I am really struggling with this one


// Network.swift

// Football Scores

//


//


import Foundation


class Network: ObservableObject {

@Published var teams: [Team] = []

}

func getTeams() {

let headers = [

"X-RapidAPI-Key": "MY API KEY",

"X-RapidAPI-Host": "api-football-v1.p.rapidapi.com"

]`

let request = NSMutableURLRequest(url: NSURL(string: "https://api-football-v1.p.rapidapi.com/v3/standings?season=2022&league=39")! 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: { (data, response, error) -> Void in

if (error != nil) {

print(error)

} else {

let httpResponse = response as? HTTPURLResponse

print(httpResponse)

DispatchQueue.main.async {

do {

let decodedTeams = try JSONDecoder().decode([Team].self, from: data)

self.team = decodedTeams

} catch let error {

print("Error decoding: ", error)

}

}

}

})

dataTask.resume()

}

and


//

// Team.swift

// Football Scores

//


//


import Foundation


struct Team: Identifiable, Decodable {

var id: Int

var name: String

var logo: String

var points: String

var goaldif: String

}



and


// Football_ScoresApp.swift

// Football Scores

//



import SwiftUI

import Foundation



@main


struct Football_ScoresApp: App {

var network = Network()

var body: some Scene {

WindowGroup {

ContentView()

.environmentObject(network)

}

}

}


and


import SwiftUI

import CoreData


struct ContentView: View {

@EnvironmentObject var network: Network



var body: some View {

ScrollView {

Text("All teams")

.font(.title).bold()

}

.onAppear {

network.getTeams()

}

VStack(alignment: .leading) {

ForEach(network.teams) { team in

HStack(alignment:.top) {

Text("\(team.id)")


VStack(alignment: .leading) {

Text(team.name)

.bold()


}

}

.frame(width: 300, alignment: .leading)

.padding()

.background(Color(#colorLiteral(red: 0.6667672396, green: 0.7527905703, blue: 1, alpha: 0.2662717301)))

.cornerRadius(20)

}

}

}


}




struct ContentView_Previews: PreviewProvider {

static var previews: some View {

ContentView()

.environmentObject(Network())

}

}


Posted on Oct 7, 2022 4:01 AM

Reply

Similar questions

1 reply

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

RapidApi in xcode

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.