Final

Anonymous
swift
12/27/2021 5:01 AM
820 B
40
Indexable
internal struct Coin: Decodable {
    internal let coinInfo: [String: CoinInfo]
    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        coinInfo = try container.decode([String: CoinInfo].self)
    }
}

internal struct CoinInfo: Decodable {
    internal let usd, usd_market_cap, usd_24h_vol, usd_24h_change: Float
    internal let last_updated_at: Int
}

do {
    let data = try JSONDecoder().decode(Coin.self, from: undecodedData)
    let coins = data.coinInfo
    let ethereumData = coins["ethereum"]
    print(ethereumData)
} catch let err {
    print(err)
}

Editor is loading...