I don't exactly know why yet, but you made me realize that I have the same bug in my code. I was just hiding it by doing it this way.
Picker("", selection: $coffeeAmount) {
ForEach(1..<21) {number in
Text(number == 1 ? "\(number) cup" : "\(number) cups")
}
}
If you use that as your Picker instead, it makes it look like it is working, but actually it is just making the picker show that 1 is selected, while coffeeAmount is actually set to zero in the background.
I have found a solution to the problem, and that is to use an id: .self parameter in the ForEach loop as shown below.
Picker("", selection: $coffeeAmount) {
ForEach(1..<21, id: \.self) {number in
Text(number == 1 ? "\(number) cup" : "\(number) cups")
}
}
That solves the problem, but I can't really give you a detailed explanation of the reason why myself.
It looks like the Picker's values are set by by the ForEach loop to start at zero and count up regardless of what numbers you actually use in your range. You can see that if you just change your range to 83..<110 and test it.
But telling the ForEach loop to use .self as its id: parameter seems to tell it to use the actual numbers in your range, rather than counting up from zero.