MonetaryExchange
Exchange
provides exchange rates between currencies. It can also convert MonetaryAmount
values into
those of different Currency
values.
Installation
Swift Package Manager
dependencies: [
.package(url: "https://github.com/SoftwareEngineerChris/MonetaryExchange.git", from: "1.0.0")
]
Decoding a Fixer.io JSON response
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.