Here is my code in Swift
import SwiftUI
struct Contentview: View {
@State private var numberOfPeople = 2
Section {
Picker("Number of Person", selection: $numberOfPeople) {
ForEach(2..<10) {
Text("\($0) people")
}
}
Text("\(numberOfPerson) people")
}
}
Number of people should be at least 2, and with a max of 10.
I bind the valuenumberOfPeople in Picker, and I show the value numberOfPeople in Text, but the actual value of numberOfPeople are alway 2 less than the value I shown in Picker
>Solution :
Approach:
- You are not tagging your views
- You need to add
tag($0)
Code:
struct ContentView: View {
@State private var numberOfPerson = 2
var body: some View {
Section {
Picker("Number of Person", selection: $numberOfPerson) {
ForEach(2..<10) {
Text("\($0) people")
.tag($0) //Fix
}
}
Text("\(numberOfPerson) people")
}
}
}
