Getting The Value From a Picker

I just started learning Swift recently and I was trying to use Pickers to get a scroll wheel of some type. I had a ForEach loop that went from 10-100 and I tried to get values back from the picker when a user selected one of those values, but I instead got the index of that value instead of the actual value. Is there a way to get the value and not the index?

Thank you
Answered by OOPer in 669812022

Yeah, here it is

Thanks.

The Italicized and bolded part is the code that I am referencing

It is very kind marking the point of interest, but code formatting would make it more readable.
Please use Code block (icon <>) feature of this site.


There may be several ways, but in your case this would work:
Code Block
Picker("Number of People:", selection: $numberOfPeople){
ForEach(1...100, id: \.self) { //<-
Text("\($0) people")
}
}

When you use ForEach(_:id:), id of each element will be used as a selection value.
Can you show your code?
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
Accepted Answer

Yeah, here it is

Thanks.

The Italicized and bolded part is the code that I am referencing

It is very kind marking the point of interest, but code formatting would make it more readable.
Please use Code block (icon <>) feature of this site.


There may be several ways, but in your case this would work:
Code Block
Picker("Number of People:", selection: $numberOfPeople){
ForEach(1...100, id: \.self) { //<-
Text("\($0) people")
}
}

When you use ForEach(_:id:), id of each element will be used as a selection value.
Thank you!
Getting The Value From a Picker
 
 
Q