I'm on #2 of the BetterRest wrap up challenge: "Replace the “Number of cups” stepper with a Picker showing the same range of values."
My first thought was a simple if/else, which works. But, it doesn't seem like the swiftUI way.
ForEach(0..<21) {
if ($0 > 0) {
Text("\($0) cup")
} else if ($0 > 1) {
Text("\($0) cups")
}
}
So, I've been trying to reduce it to one Text().
This produces a list with 0 cups at the top.
Text(($0 > 0) ? "\($0) cup" : "\($0) cups")
And, this leaves it blank, but still leaves a blank spot at the top.
Text(($0 > 0) ? "\($0) cup" : ($0 > 1) ? "\($0) cups" : "")
Searching on the topic I found other people being led into localization for this:
https://www.hackingwithswift.com/forums/swiftui/betterrest-number-of-cups-picker-question/8678
But, that seems pretty far out of the scope of this beginner tutorial. No?
I also tried to just change my ForEach range to 1...20. But, then it started throwing erreors. And, using 1..<21, it began adding wrong. If someone could explain why, maybe that would help.
Edit: This is weird. Now the if/else is NOT working and they're all displayed as cup. What the? I was just looking at all the plurals a few minutes ago.
And, now there's some green bar overlaying part of my code that says something about a breakpoint that I can't get rid of. Sigh
Edit 2: Now it works again with the if/else. Nothing changed so I guess xcode is a little glitchy.