Wednesday, September 28, 2022
HomeiOS Developmentios - SwiftUI : Cannot obtain API response

ios – SwiftUI : Cannot obtain API response


I am attempting to get a response from this hyperlink : https://zsr.octane.gg/gamers/5f5ae840c6cbf591c568a477 nevertheless it will not work and I can not determine why.

There may be my ContentView.swift :

struct ContentView: View {
    
    @State personal var gamers = [Player]()
    
    var physique: some View {
        Record(gamers, id: ._id) { merchandise in
                    VStack(alignment: .main) {
                        Textual content(merchandise.title)
                            .font(.headline)
                        Textual content(merchandise.nation)
                    }
                }.activity {
            await loadData()
        }
    }
    
    func loadData() async {
        guard let url = URL(string: "https://zsr.octane.gg/gamers/5f5ae840c6cbf591c568a477") else {
            print("URL invalide")
            return
        }
        
        do {
            let (knowledge, _) = attempt await URLSession.shared.knowledge(from: url)
            
            if let decodedResponse = attempt? JSONDecoder().decode(Response.self, from: knowledge) {
                gamers = decodedResponse.gamers
            }
        } catch {
            print("Invalid knowledge")
        }
    }
}

My Response struct :

struct Response: Codable {
    var gamers: [Player]
}

Participant struct :

struct Participant: Codable {
    var _id: String
    var slug: String
    var tag: String
    var title: String
    var nation: String
    var workforce: Staff
    var accounts: [Account]
    var revelant: Bool
}

Staff struct :

struct Staff: Codable {
    var _id: String
    var slug: String
    var title: String
    var area: String
    var picture: String
    var related: Bool
}

Account struct :

struct Account: Codable {
    var platform: String
    var id: String
}

I adopted a tutorial which works effectively however after I substitute the hyperlink and use my very own structs nothing occurs after I launch the app.
Thanks in your assist.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments