Exchange
public struct Exchange : Decodable
Exchange
provides exchange rates between currencies. It can also convert MonetaryAmount
values into
those of different Currency
values.
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.
-
Represents a monetary exchange rate between two currencies. Rounded to six decimal places.
Declaration
Swift
public typealias Rate = RoundedDecimal<Places.six>
-
The base currency for this
Exchange
. The currency to which all rates from this exchange are based upon.Declaration
Swift
public let base: Currency
-
Creates a new
Exchange
from a baseCurrency
and a collection of exchange rates.Declaration
Swift
public init(base: Currency, rates: [Currency : Rate])
-
Throws
If the exchange has no exchange rate for thetargetCurrency
, this method will throwExchangeError.noRateError
Declaration
Swift
public func rate(from originCurrency: Currency, targetCurrency: Currency) throws -> Rate
Parameters
originCurrency
The
base
Currency
in the pair.targetCurrency
The
quote
Currency
in the pair.Return Value
The exchange rate for the provided currency pair.
-
Throws
If the exchange has no exchange rate for thetargetCurrency
, this method will throwExchangeError.noRateError
Declaration
Swift
public func convert(monetaryAmount: MonetaryAmount, to targetCurrency: Currency) throws -> MonetaryAmount
Parameters
monetaryAmount
A
MonetaryAmount
which has aCurrency
to be converted to thetargetCurrency
targetCurrency
The
quote
Currency
in the pair (the base being that in themonetaryAmount
parameter).Return Value
A
MonetaryAmount
representing the original amount converted to the target currency using theExchange
exchange rate.
-
Adds support for requesting data from the Fixer.io Latest Rates API and being provided with an
Exchange
instance configured with this data.Example
See moreExchange.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. } }
Declaration
Swift
struct Fixer
-
An
Exchange
can be decoded directly from a standard standard Fixer.io Latest Rates JSON response. See Fixer API Documentation for more information about its API usage.Example
let exchange = try? JSONDecoder().decode(Exchange.self, from: fixerResponseData)
Declaration
Swift
public init(from decoder: Decoder) throws
-
Returns a table of base/quote exchange rates
For example:
EUR/KMF = 492.307567 EUR/COP = 3780.992416 EUR/DOP = 57.558938 ...
Declaration
Swift
public var description: String { get }