使用Swift词典中的键值映射索引位置

时间:2018-06-02 20:29:55

标签: json swift

根据我的模型,我能够 print(crypto, name, price ?? 0.0, totalSupply ?? 0.0)
// BTC Bitcoin 7639.39 17070625.0
// ETH Ethereum 590.943 99826611.0
// all 99 currencies

我被迫在API中打印出所有99种货币。我希望能够 cherry-pick 特定货币。

字典“1”对应于BTC,字典“1021”对应于ETH。这些与“id”键相同,但它们具有相同的数值。

最好只找到“id”元素然后使用一个条件,说明是否找到键值对"id":1"id":1027打印该字典中的所有值?我需要映射吗?一个简单的条件?

目标是控制选择一个货币,即 ETH / 1027 并打印一行,最后将在某种表中放置。<登记/> BTC | Bitcoin | 7639.39 | 17070625.0


控制器

for (crypto, base) in rawResponse.data {
                    if let name = base.name {
                        let price = base.quotes["USD"]?.price
                        let totalSupply = base.totalSupply
                        print(crypto, name, price ?? 0.0, totalSupply ?? 0.0)   
                    }

                }

JSON模型

struct RawServerResponse : Codable {
var data = [String:Base]()

private enum CodingKeys: String, CodingKey {
    case data
}

init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    let baseDictionary = try container.decode([String:Base].self, forKey: .data)
    baseDictionary.forEach { data[$0.1.symbol] = $0.1 }
}
}

struct Base : Codable {
let name : String?
let symbol : String
let quotes : [String: Quotes]
}

struct Quotes : Codable {
let price : Double?
let volume24H : Double?
}

JSON

"data": {
    "1": {
        "id": 1, 
        "name": "Bitcoin", 
        "symbol": "BTC", 
        "website_slug": "bitcoin", 
        "rank": 1, 
        "circulating_supply": 17069825.0, 
        "total_supply": 17069825.0, 
        "max_supply": 21000000.0, 
        "quotes": {
            "USD": {
                "price": 7663.22, 
                "volume_24h": 4898370000.0, 
                "market_cap": 130809824337.0, 
                "percent_change_1h": 2.1, 
                "percent_change_24h": 1.17, 
                "percent_change_7d": 2.17
            }
        }, 
        "last_updated": 1527925172
    }, 
    "1027": {
        "id": 1027, 
        "name": "Ethereum", 
        "symbol": "ETH", 
        "website_slug": "ethereum", 
        "rank": 2, 
        "circulating_supply": 99817109.0, 
        "total_supply": 99817109.0, 
        "max_supply": null, 
        "quotes": {
            "USD": {
                "price": 589.299, 
                "volume_24h": 1841640000.0, 
                "market_cap": 58822122295.0, 
                "percent_change_1h": 1.9, 
                "percent_change_24h": 0.93, 
                "percent_change_7d": -0.96
            }
        }

1 个答案:

答案 0 :(得分:1)

我实际上建议您使用不同的API,因为这种响应格式看起来并不真正开发。但是,如果你必须使用它,这里有一个快速解决方法,你可以选择一个项目:

if let eth = data.filter({ (item) -> Bool in
    guard let crypto = item.value as? Base else {
        return false
    }
    return crypto.symbol == "ETH"
}).first {
    print(eth)
}

if let btc = data.filter({ (item) -> Bool in
    guard let crypto = item.value as? Base else {
        return false
    }
    return crypto.symbol == "BTC"
}).first {
    print(btc)
}