Create a Currency Convert app that can convert from US dollars (USD) to Canadian dollars (CAD) and Mexican Pesos (MXN).
Get the currency conversion value from Google:
On August 7th, 2019 they were:
$1 USD to $1.33 CAD
$1 USD to $19.70 MXN
Currency Converter App
-
Create a currency type enum above the class and below the
importstatements.enum Currency { case cad case mxn }
-
Create a property named
currencyof typeCurrency. This will store the current currency type we'll be converting to. Set an initial value of.cad. -
In the
cadButtonTappedaction:- Toggle this button's
isSelectedstate - Toggle the mxn button's
isSelectedstate - Check this button's state, and if
true:- set the
currencyproperty to.cad - Display the currency in the
toCurrencyLabel(so have it say "Currency (CAD)")
- set the
- Toggle this button's
-
In the
mxnButtonTappedaction:- Toggle this button's
isSelectedstate - Toggle the cad button's
isSelectedstate - Check this button's state, and if
true:- set the
currencyproperty to.mxn - Display the currency in the
toCurrencyLabel(so have it say "Currency (MXN)")
- set the
- Toggle this button's
-
Create a helper method to calculate the currency based on the Currency using the method signature:
func convert(_ dollars: Double) -> Double { }
-
In the above method:
- Check the value of
currencyto see whether you should convert to CAD or MXN - Perform the conversion with the dollars passed into this method
- Return the converted value
- Check the value of
-
In the
convertButtonTapped()action method:- use a
guard letto get user input - convert the dollar amount to the expected currency (hint, you'll want to call the
convertmethod you created in step 5) - Update the
toCurrencyTextField.textproperty with the converted currency value
- use a
-
Customize the output display using a
NumberFormattervar currencyFormatter: NumberFormatter = { let formatter = NumberFormatter() formatter.numberStyle = .currency return formatter }()
-
Use the
string(from:)method to convert from a number to a String for display
