altela
altela

Reputation: 354

Parse JSON Result To UILabel in Swift

I'm really new into swift and currently learning API by doing a project that shows list of games from rawg.io referring to the website's doc. I created GameFeed.swift & GameDetail.swift to pull name, release date, and rating from it and working fine in my console.

GameFeed.swift :

struct GameFeed: Codable {
    let results:[GameDetail]
}

GameDetail.swift :

struct GameDetail: Codable {
    let name:String
    let released:String
    let rating:Double
}

Result in Console

Now i'm trying to put the results to a simple UIlabel like gameName.text, gameReleased.text & gameRating.text from ViewController.swift so it will be show in Main.Storyboard

enter image description here

I did research on Google about how to show it to these UIlabel by using DispatchQueue.main.async but when I'm declaring it, it receiving error:

Value of type 'GameFeed' has no member 'name'

same error messages also happened to released and rating. This is my ViewController.Swift :

class ViewController: UIViewController {
    @IBOutlet weak var gameName: UILabel!
    @IBOutlet weak var gameReleased: UILabel!
    @IBOutlet weak var gameRating: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Defining API Site
        let urlString = "https://api.rawg.io/api/games"
            let url = URL(string: urlString)
                guard url != nil else {
            return
        }
        
        // Calling API
        let session = URLSession.shared
            let dataTask = session.dataTask(with: url!){
                (data, response, error) in
            
                    if error == nil && data != nil {
                        let decoder = JSONDecoder()
                        do {
                            let gameFeed = try decoder.decode(GameFeed.self, from: data!)
                            print(gameFeed)
                            DispatchQueue.main.async {
                                self.gameName.text = gameFeed.name
                                self.gameReleased.text = gameFeed.released
                                self.gameRating.text = gameFeed.rating
                           }
                }
                        catch {
                            print("Error Parsing JSON")
                }
        }
        }
        dataTask.resume()
    }
}

What should I do to make it possible to parse the data to labels?

Upvotes: 0

Views: 407

Answers (1)

Mojtaba Hosseini
Mojtaba Hosseini

Reputation: 119310

The GameFeed contains an Array of GameDetails. But you are trying to set a single GameDetail on those labels. You should first pull out a single GameDetail from that array, then assign it in a way you like.

DispatchQueue.main.async {
    let gameDetail = gameFeed.results.first // <- This will return the first one
    self.gameName.text = gameDetail?.name
    self.gameReleased.text = gameDetail?.released
    self.gameRating.text = gameDetail?.rating
}

Upvotes: 1

Related Questions