简体   繁体   中英

Parse JSON Result To UILabel in Swift

I'm really new into swift & 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
}

控制台中的结果

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

在此处输入图像描述

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 & 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?

The GameFeed contains an Array of GameDetail s. 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM