debugDescription: “Expected to decode Array but found a dictionary instead.”, underlyingError: nil)

前端 未结 3 672
粉色の甜心
粉色の甜心 2021-01-13 06:48

I want to load an online json file into my application, but I am running into this error:

typeMismatch(Swift.Array, Swift.DecodingError.Context(codi

3条回答
  •  忘掉有多难
    2021-01-13 07:32

    Correct Answer is done previously from my two friends

    but you have to do it better i will provide solution for you to make code more clean and will give you array of Dates

    here is your model with codable

       import Foundation
    
    struct Initial: Codable {
        let copyright: String
        let totalItems: Int
        let totalEvents: Int
        let totalGames: Int
        let totalMatches: Int
        let wait: Int
        let dates: [Dates]
    }
    
    struct Dates: Codable {
        let date: String
        let totalItems: Int
        let totalEvents: Int
        let totalGames: Int
        let totalMatches: Int
        let games: [Game]
    }
    
    struct Game: Codable {
        let gamePk: Int
        let link: String
        let gameType: String
        let season: String
        let gameDate: String
        let status: Status
        let teams: Team
        let venue: Venue
        let content: Content
    }
    
    struct Status: Codable {
        let abstractGameState: String
        let codedGameState: Int
        let detailedState: String
        let statusCode: Int
        let startTimeTBD: Bool
    }
    
    struct Team: Codable {
        let away: Away
        let home: Home
    }
    
    struct Away: Codable {
        let leagueRecord: LeagueRecord
        let score: Int
        let team: TeamInfo
    }
    
    struct Home: Codable {
        let leagueRecord: LeagueRecord
        let score: Int
        let team: TeamInfo
    }
    
    struct LeagueRecord: Codable {
        let wins: Int
        let losses: Int
        let type: String
    }
    
    struct TeamInfo: Codable {
        let id: Int
        let name: String
        let link: String
    }
    
    struct Venue: Codable {
        let name: String
        let link: String
    }
    
    struct Content: Codable {
        let link: String
    }
    
    // MARK: Convenience initializers
    
    extension Initial {
        init(data: Data) throws {
            self = try JSONDecoder().decode(Initial.self, from: data)
        }
    
    }
    

    And Here is Controller Will make solution more easy

    class ViewController: UIViewController {
        var todaysGamesURL: URL = URL(string: "https://statsapi.web.nhl.com/api/v1/schedule")!
    
        var gameData: Initial?
        let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
    
    
    
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.loadTodaysGames()
    
        }
    
            func loadTodaysGames(){
        print("load Games")
        let todaysGamesDatatask = URLSession.shared.dataTask(with: todaysGamesURL, completionHandler: dataLoaded)
        todaysGamesDatatask.resume()
    }
    func dataLoaded(data:Data?,response:URLResponse?,error:Error?){
        if let detailData = data {
            if  let inital = try? Initial.init(data: detailData){
               print(inital.dates)
            }else{
                 // print("Initial")
            }
        }else{
            print(error!)
        }
    }
    
    }
    

提交回复
热议问题