Yeah, here it is
import SwiftUI
struct ContentView: View {
@State private var checkoutAmount=""
@State private var numberOfPeople=1
@State private var tipPercentage=1
let tipPercentages=[10,15,20,25,0]
var totalPerPerson: Double {
let peopleCount=Double(numberOfPeople+1)
let tipSelection=Double(tipPercentages[tipPercentage])
let orderAmount=Double(checkoutAmount) ?? 0
let tipValue=orderAmount/100*tipSelection
let grandTotal=orderAmount+tipValue
let amountPerPerson=grandTotal/peopleCount
return amountPerPerson
}
var body: some View {
NavigationView{
Form{
Section{
TextField("Amount:", text:$checkoutAmount)
.keyboardType(.decimalPad)
Picker("Number of People:", selection: $numberOfPeople){
ForEach(1..101){
Text("\($0) people")
}
}
}
Section(header: Text("Tip Percentage:")){
Picker("Tip Percentage:", selection: $tipPercentage) {
ForEach(0..tipPercentages.count) {
Text("\(self.tipPercentages[$0])%")
}
}
.pickerStyle(SegmentedPickerStyle())
}
Section{
Text("$\(totalPerPerson,specifier: "%.2f")")
}
}
.navigationBarTitle("WeSplit", displayMode: .inline)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
It's just a simple project that takes a bill, gets the number of people, the tip, and then calculates the final price each person has to pay.
This code works, but that's only because I added a +1 to the numberOfPeople variable since it took the index instead of the value, but I was just wondering if there was a way to get the value instead.
The Italicized and bolded part is the code that I am referencing