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
Exchangefrom a baseCurrencyand 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.noRateErrorDeclaration
Swift
public func rate(from originCurrency: Currency, targetCurrency: Currency) throws -> RateParameters
originCurrencyThe
base
Currencyin the pair.targetCurrencyThe
quote
Currencyin 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.noRateErrorDeclaration
Swift
public func convert(monetaryAmount: MonetaryAmount, to targetCurrency: Currency) throws -> MonetaryAmountParameters
monetaryAmountA
MonetaryAmountwhich has aCurrencyto be converted to thetargetCurrencytargetCurrencyThe
quote
Currencyin the pair (the base being that in themonetaryAmountparameter).Return Value
A
MonetaryAmountrepresenting the original amount converted to the target currency using theExchangeexchange rate.
-
Adds support for requesting data from the Fixer.io Latest Rates API and being provided with an
Exchangeinstance 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
Exchangecan 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 }
View on GitHub
Install in Dash
Exchange Structure Reference