An Exchange can be decoded directly from a the Fixer.io Latest Rates JSON response.
See Fixer API Documentation for more information about its API usage.
Example using the Fixer Extension
Exchange.Fixer.exchange(accessKey: "YourFixerAccessKey") { result in
switch result {
case let .success(exchange):
// We have an Exchange value
case let .failure(error):
// Something went wrong. Dig into the error.
}
}
See the documentation for Exchange.Fixer for more information.
Example using JSONDecoder Directly
let exchange = try? JSONDecoder().decode(Exchange.self, from: fixerResponseData)
Alternatively, an Exchange can be constructed with a base currency and a dictionary of currency-rate pairs.
Cross-rates
If converting between two currencies which neither are the base currency, but each have a rate against the base currency,
then a cross-rate will be produced.
For example, if the base currency is EUR but a rate for GBP to USD is requested, a cross-rate will be used. i.e. GBP to EUR to USD.
See the Collins Dictionary definition of Cross-Rate
for more information.
MonetaryExchange
Exchangeprovides exchange rates between currencies. It can also convertMonetaryAmountvalues into those of differentCurrencyvalues.Installation
Swift Package Manager
Decoding a Fixer.io JSON response
An
Exchangecan be decoded directly from a the Fixer.io Latest Rates JSON response. See Fixer API Documentation for more information about its API usage.Example using the Fixer Extension
See the documentation for
Exchange.Fixerfor more information.Example using JSONDecoder Directly
Alternatively, an
Exchangecan be constructed with a base currency and a dictionary of currency-rate pairs.Cross-rates
If converting between two currencies which neither are the base currency, but each have a rate against the base currency, then a cross-rate will be produced.
For example, if the base currency is EUR but a rate for GBP to USD is requested, a cross-rate will be used. i.e. GBP to EUR to USD.
See the Collins Dictionary definition of Cross-Rate for more information.