Challenge
- Add a header to the third section, saying “Amount per person”
Section {
Text(totalPerPerson, format: currencyFormat)
} header: {
Text("Amount per person")
}
- Add another section showing the total amount for the check – i.e., the original amount plus tip value, without dividing by the number of people.
var totalAmount: Double {
checkAmount + (checkAmount / 100 * Double(tipPercentage))
}
Section {
Text(totalAmount, format: currencyFormat)
} header: {
Text("Total amount")
}
- Change the tip percentage picker to show a new screen rather than using a segmented control, and give it a wider range of options – everything from 0% to 100%. Tip: use the range
0..<101 for your range rather than a fixed array.
Picker("Number of people", selection: $numberOfPeople) {
ForEach(2..<100) {
Text("\\($0) people")
}
}
- Make a new property to store the currency formatter(
.currency(code: Locale.current.currencyCode ?? "USD")).
var currencyFormat: FloatingPointFormatStyle<Double>.Currency {
.currency(code: Locale.current.currency?.identifier ?? "USD")
}
Section {
Text(totalAmount, format: currencyFormat)
}