0

I want to get the home_name of the first team. But it shows me blank. This is my JSON fetching Code:

class NetworkManager: ObservableObject {
    
    @Published var posts = [Post]()
    
    static var test = ""
    
    func fetchData() {
        if let url = URL(string: "https://livescore-api.com/api-client/teams/matches.json?number=10&team_id=19&key=##KEY##&secret=##SECRET##&number=7&team_id=46") {
            let session = URLSession(configuration: .default)
            let task = session.dataTask(with: url) { (gettingInfo, response, error) in
                if error == nil {
                    let decoder = JSONDecoder()
                    if let safeData = gettingInfo {
                        do {
                            let results = try decoder.decode(Results.self, from: safeData)
                            DispatchQueue.main.async {
                                self.posts = results.data
                                NetworkManager.test = results.data[0].home_name
                            }
                        } catch {
                            print(error)
                        }
                    }
                }
            }
            task.resume()
        }
    }
}

My struct:

import Foundation

struct Results: Decodable {
    let data: [Post]
}

struct Post: Decodable, Identifiable {
    var id: String {
        return objectID
    }
    let objectID: String
    let home_name: String
    let away_name: String
}

And my view but the view shouldn't be the problem.

struct ContentView: View {
    
    @ObservedObject var networkManager = NetworkManager()
    
    var body: some View {
        Text(NetworkManager.test)
            .onAppear {
                self.networkManager.fetchData()
            }
        
    }
}
5
  • Did you check what results contains? Commented Sep 18, 2020 at 11:04
  • 2
    test isn't marked as Published Commented Sep 18, 2020 at 11:06
  • Shouldn't it be @Published var test = "" Commented Sep 18, 2020 at 11:12
  • I tried to edit your post to hide the actual api_key and secret. That's very bad to share it online for everyone to grab!! Commented Sep 18, 2020 at 13:51
  • Thanks guys it works now. Published was right. Commented Sep 19, 2020 at 6:13

1 Answer 1

1

Here is the working code

struct Results: Decodable {
    let data: [Post]
}

struct Post: Decodable, Identifiable {
    let id: String
    var objectID: String {
        return id
    }
    let home_name: String
    let away_name: String
}

class NetworkManager: ObservableObject {
    
    @Published var posts = [Post]()
    
    @Published var test = ""
    
    func fetchData() {
        if let url = URL(string: "https://livescore-api.com/api-client/teams/matches.json?number=10&team_id=19&keykey_here&secret=secret_here&number=7&team_id=46") {
            let session = URLSession(configuration: .default)
            let task = session.dataTask(with: url) { (gettingInfo, response, error) in
                if error == nil {
                    let decoder = JSONDecoder()
                    if let safeData = gettingInfo {
                        do {
                            let results = try decoder.decode(Results.self, from: safeData)
                            DispatchQueue.main.async {
                                self.posts = results.data
                                self.test = results.data[0].home_name
                            }
                        } catch {
                            print(error)
                        }
                    }
                }
            }
            task.resume()
        }
    }
}

struct ContentView: View {
    @ObservedObject var networkManager = NetworkManager()
    
    var body: some View {
        Text(networkManager.test)
            .onAppear {
                self.networkManager.fetchData()
            }
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

You have made two mistakes

  1. marked test as static instead of marking it as @Published
  2. Post - here you are returning id from objectId instead you should have id from the JSON and you have to return objectID from id value.
Sign up to request clarification or add additional context in comments.

2 Comments

@saurabh Prajapati hide the OP's shared key and secret from the request. Most probably he is new into programming and doesn't know the security issues with sharing his paid key.
I try to use this now with my widget. But it stays blank. Any Idea? Is the Widget with WidgetKit working different?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.