动态改变基础货币

Dynamically Changing the Base Currency

我有一个货币API,returns一个JSON对象包含一个奇怪的安排:基础货币被用作标签。典型的货币 API 有“基础”、“日期”、“成功”和“汇率”等标签,但这个 API 没有任何这些标签。

{
    "usd": {
        "aed": 4.420217,
        "afn": 93.3213,
        "all": 123.104693,
        "amd": 628.026474,
        "ang": 2.159569,
        "aoa": 791.552347,
        "ars": 111.887966,
        "aud": 1.558363,
        "awg": 2.164862,
        "azn": 2.045728,
        "bam": 1.9541,
        "bbd": 2.429065,
        "bch": 0.001278
    }
}

顶部的“usd”(美元)称为基础货币或本国货币。目前,存储结构和状态参数被硬编码为“usd”,这可以防止使用与其他基础货币的汇率。汇率 API 对美元的基础货币非常有效。

我需要帮助修改内容,以便我可以下载不同基础货币的汇率。例如,我可以在存储模型和状态参数中使用字符串变量吗?任何启发将不胜感激。

struct RateResult: Codable {
        let usd: [String: Double]
    }    

   @State private var  results = RateResult(usd: [:])


struct ContentView: View {

   var body: some View {

   }

   func UpdateRates() {

      let baseUrl = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/"
        let baseCur = baseCurrency.baseCur.baseS   // usd
        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) { data, response, error in
            if let data = data {
                if let decodedResponse = try? JSONDecoder().decode(RateResult.self, from: data) {
                    
                    // we have good data – go back to the main thread
                    DispatchQueue.main.async {
                        
                        // update our UI
                        self.results = decodedResponse
                        
                    // save off currency exchange rates
                    
                    }
                    // everything is good, so we can exit
                    return
                }
            }
            // if we're still here it means there was a problem
            print("Currency Fetch Failed: \(error?.localizedDescription ?? "Unknown error")")
        }.resume()
    }
}
import SwiftUI
//You can't use the standard Codable for this. You have to make your own.
class BaseCurrency: Codable {
    let id = UUID()
    var baseCurrencies: [String : [String: Double]] = [:]
    required public init(from decoder: Decoder) throws {
        do{
            print(#function)
            let baseContainer = try decoder.singleValueContainer()
            let base = try baseContainer.decode([String : [String: Double]].self)
            for key in base.keys{
                baseCurrencies[key] = base[key]
            }
        }catch{
            print(error)
            throw error
        }
    }
    //@State should never be used outside a struct that is a View
}
struct CurrencyView: View {
    @StateObject var vm: CurrencyViewModel = CurrencyViewModel()
    
    var body: some View {
        VStack{
            List{
                if vm.results != nil{
                    ForEach(vm.results!.baseCurrencies.sorted{[=10=].key < .key}, id: \.key) { key, baseCurrency in
                        DisclosureGroup(key){
                            ForEach(baseCurrency.sorted{[=10=].key < .key}, id: \.key) { key, rate in
                                HStack{
                                    Text(key)
                                    Text(rate.description)
                                }
                            }
                        }
                    }
                }else{
                    Text("waiting...")
                }
            }
            //To select another rate to go fetch
            RatesPickerView().environmentObject(vm)
        }.onAppear(){
            vm.UpdateRates()
        }
    }
}
struct RatesPickerView: View {
    @EnvironmentObject var vm: CurrencyViewModel
    var body: some View {
        if vm.results != nil{
            //You can probaly populate this picker with the keys in
            // baseCurrency.baseCur.baseS
            Picker("rates", selection: $vm.selectedBaseCurrency){
                ForEach((vm.results!.baseCurrencies.first?.value.sorted{[=10=].key < .key})!, id: \.key) { key, rate in
                    Text(key).tag(key)
                }
            }
        }else{
            Text("waiting...")
        }
    }
}
class CurrencyViewModel: ObservableObject{
    
    @Published var results: BaseCurrency?
    @Published var selectedBaseCurrency: String = "usd"{
        didSet{
            UpdateRates()
        }
    }
    init() {
        //If you can .onAppear you don't need it here
        //UpdateRates()
    }
    func UpdateRates() {
        print(#function)
        let baseUrl = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/"
        let baseCur = selectedBaseCurrency   // usd
        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) { data, response, error in
            if let data = data {
                do{
                    let decodedResponse = try JSONDecoder().decode(BaseCurrency.self, from: data)
                    
                    DispatchQueue.main.async {
                        
                        if self.results == nil{
                            //Assign a new base currency
                            self.results = decodedResponse
                        }else{ //merge the existing with the new result
                            for base in decodedResponse.baseCurrencies.keys{
                                self.results?.baseCurrencies[base] = decodedResponse.baseCurrencies[base]
                            }
                        }
                        //update the UI
                        self.objectWillChange.send()
                    }
                    
                }catch{
                    //Error thrown by a try
                    print(error)//much more informative than error?.localizedDescription
                }
            }
            if error != nil{
                //data task error
                print(error!)
            }
        }.resume()
    }
}
struct CurrencyView_Previews: PreviewProvider {
    static var previews: some View {
        CurrencyView()
    }
}