将键/值数据复制到结构中

Posted

技术标签:

【中文标题】将键/值数据复制到结构中【英文标题】:Copying Key / Value Data into Structure 【发布时间】:2021-06-11 00:16:54 【问题描述】:

我在将 API 汇率加载到结构实例 moneyRate 时遇到问题。速率中的数据格式为键/值 [String : Double]。在 URLSession 的末尾,通知我已将日期和基础货币复制到 moneyRates 中,但我似乎无法对诸如 moneyRates.rates = rates 之类的汇率做同样的事情。我可以在控制台中看到速率数据。

struct MoneyRates 
    var date: String
    var base: String
    var rates: [String: Double]


class CurrencyRates: ObservableObject 
    
    var moneyRates = MoneyRates(date: "2020-07-04", base: "usd", rates: ["bzd": 0.0, "cnh": 0.0, "bbd": 0.0, "hkd": 0.0, "bmd": 0.0, "amd": 0.0, "inr": 0.0, "gmd": 0.0, "ang": 0.0, "egp": 0.0, "cdf": 0.0, "bif": 0.0, "lak": 0.0, "cop": 0.0, "cve": 0.0, "gtq": 0.0, "hrk": 0.0])
    
    // retrieve exchange rates for all 150+ countries from internet and save to rateArray
    func updateRates(baseCur: String) 
        
        print("doing update")
        
        let baseUrl = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/"
        let requestType = ".json"
        
        guard let url = URL(string: baseUrl + baseCur + requestType) else 
            print("Invalid URL")
            return
        
        let request = URLRequest(url: url)
        
        URLSession.shared.dataTask(with: request)  [self] data, response, error in
            
            if let data = data 
                do 
                    let result = try JSONSerialization.jsonObject(with: data) as! [String:Any]
                    
                    var keys = Array(result.keys)
                    if let dateIndex = keys.firstIndex(of: "date"),
                       let date = result[keys[dateIndex]] as? String, keys.count == 2 
                        keys.remove(at: dateIndex)
                        let base = keys.first!
                        let rates = MoneyRates(date: date, base: base, rates: result[base] as! [String:Double])
                        
                       
                        moneyRates.date = date
                        moneyRates.base = base
                        print(rates) // displays all key / value exchange rates
                     
                                               
                    
                    
                 catch 
                    print(error)
                
            
        .resume()
    

【问题讨论】:

【参考方案1】:

在第一次分析时,我认为我可以设置 moneyRates.rates = rates

但是当这不起作用时,我在上面查看了 basedate 是如何从 result 中提取出来的,并认为我可以对汇率做同样的事情。

所以解决方案是 moneyRates.rates = result[base] as! [字符串:双]

class CurrencyRates: ObservableObject 
    
    var moneyRates = MoneyRates(date: "2020-07-04", base: "usd", rates: ["bzd": 0.0, "cnh": 0.0, "bbd": 0.0, "hkd": 0.0, "bmd": 0.0, "amd": 0.0, "inr": 0.0, "gmd": 0.0, "ang": 0.0, "egp": 0.0, "cdf": 0.0, "bif": 0.0, "lak": 0.0, "cop": 0.0, "cve": 0.0, "gtq": 0.0, "hrk": 0.0])
    
    // retrieve exchange rates for all 150+ countries from internet and save to rateArray
    func updateRates(baseCur: String) 
        
        print("doing update")
        
        let baseUrl = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/"
        let requestType = ".json"
        
        guard let url = URL(string: baseUrl + baseCur + requestType) else 
            print("Invalid URL")
            return
        
        let request = URLRequest(url: url)
        
        URLSession.shared.dataTask(with: request)  [self] data, response, error in
            
            if let data = data 
                do 
                    let result = try JSONSerialization.jsonObject(with: data) as! [String:Any]
                    
                    var keys = Array(result.keys)
                    if let dateIndex = keys.firstIndex(of: "date"),
                       let date = result[keys[dateIndex]] as? String, keys.count == 2 
                        keys.remove(at: dateIndex)
                        let base = keys.first!
                   
                        moneyRates.date = date
                        moneyRates.base = base
                        moneyRates.rates = result[base] as! [String : Double]
                     
                                               
                    
                    
                 catch 
                    print(error)
                
            
        .resume()
    

【讨论】:

以上是关于将键/值数据复制到结构中的主要内容,如果未能解决你的问题,请参考以下文章

如何将键值对添加到字典中? [复制]

使用支持对象/结构的泛型时如何返回空值/默认值? [复制]

二表之间的数据复制 mysql

oracle中如何将数据从一个表复制到另一个表(仅复制其中某些字段)

mysql 数据库怎么复制一张表?

将数据复制到同一张表和从同一表复制数据,并将复制数据的一列中的值更改为指定值