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 possible errors thrown by an Exchange

    See more

    Declaration

    Swift

    public enum ExchangeError : Error
  • The base currency for this Exchange. The currency to which all rates from this exchange are based upon.

    Declaration

    Swift

    public let base: Currency
  • A dictionary of Currency values to Rate values where the Rate is the exchange rate between the base Currency and the queried Currency.

    Declaration

    Swift

    public let rates: [Currency : Rate]
  • Creates a new Exchange from a base Currency and a collection of exchange rates.

    Declaration

    Swift

    public init(base: Currency, rates: [Currency : Rate])

    Parameters

    base

    The base currency for this Exchange. The currency to which all rates from this exchange are based upon.

    rates

    A dictionary of Currency values to Rate values where the Rate is the exchange rate between the base Currency and the queried Currency.

  • Throws

    If the exchange has no exchange rate for the targetCurrency, this method will throw ExchangeError.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 the targetCurrency, this method will throw ExchangeError.noRateError

    Declaration

    Swift

    public func convert(monetaryAmount: MonetaryAmount, to targetCurrency: Currency) throws -> MonetaryAmount

    Parameters

    monetaryAmount

    A MonetaryAmount which has a Currency to be converted to the targetCurrency

    targetCurrency

    The quote Currency in the pair (the base being that in the monetaryAmount parameter).

    Return Value

    A MonetaryAmount representing the original amount converted to the target currency using the Exchange 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

    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 more

    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 }