UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: BetterRest "number of cups" Picker question

Forums > SwiftUI

Hi all,

I am working on replacing the "number of cups" stepper with a picker showing the range of values 1..20. I personall prefer the stepper approach as you stay on the same page as all the other information and don't have to navigate back and forth, but i digress. anyway, i wrote the following code so that 1 is for singular cup, and then 2-20 are plural (cups):

''' Picker("Coffee Amount", selection: $coffeeAmount) { ForEach(1..<2) { Text("($0) cup") }

ForEach(2..<20) { Text("($0) cups") } }

'''

In writing it this way, the picker does indeed correctly show 1 cup, and then 2 cups, 3 cups etc. However, i see a checkmark at both 1 cup and 2 cups, and whenever i try to select 2 cups it just defaults the daily coffee intake to 1 cup. it works fine for selecting cups 3-20. Just wondering where my logic is going wrong. Separately, I tried incorporating if clauses in the same way we wrote the stepper, but it won't accept that approach for the picker, even if I just write:

''' Picker("Coffee Amount", selection: $coffeeAmount) { ForEach(1..<20) { if coffeeAmount == 1 { Text(" 1 cup") } else { Text("(coffeeAmount) cups") } } } '''

Appreciate any thoughts/suggestions

3      

You should go with a Localizable.stringsdict.

https://medium.com/@vitaliikuznetsov/plurals-localization-using-stringsdict-in-ios-a910aab8c28c

You don't need necessarily more than one language for this.

3      

Thanks for the link.

Apologies, but after reading through it, I am as lost as before as have little to no idea as to how to incorporate it in the code I wrote.

I am having great dificulty in relating it to the Picker code that I wrote, and am sensing that I need to write a completely new code, rather than amending the code I wrote.

3      

Hm, the nice thing about using Text() is that you don't need to implement that in code. Create a Localizable.stringsdict via File -> New -> stringsdict and name it exactly Localizable.stringsdict. Create a new entry. In the link above there is an example. Use this and replace VARIABLE with cup. SwiftUI will do the rest for you because Text not only accepts a String but also a LocalizedStringKey which it will use automatically on demand. Nothing to do for you.

For further reading https://swiftwithmajid.com/2019/10/16/localization-in-swiftui/

I haven't found a link here on hackingwithswift but if you are a member of hackingwithswift+ than I know there is a whole section in the UltimatePortfolioApp course Paul is currently working on.

3      

thanks for the guidance.

will work on trying to implement stringsdict.

at the end of the day, as someone faily new to SwiftUI, and not grasping stringsdict, I find Stepper far easier as I can use if/else clauses much in the same way as when I wrote python programs.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.