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

SOLVED: BetterRest - Day 28 Challenge 2 - How to create a simple integer picker

Forums > 100 Days of SwiftUI

I can't see what I am doing wrong but in Challenge 2 Day 28 I tried to: Replace the “Number of cups” stepper with a Picker showing the same range of values.

So I already have the coffee amount @State variable:

 @State private var coffeeAmount = 1.0

In the body I have this picker and a Text to test the output of the picker selection

                Section(header: Text("Daily coffee intake? - Picker")){
                    Picker("Number of cups", selection: $coffeeAmount) {
                        ForEach(1..<21) {
                            if $0 > 1 {
                                Text("\($0) cups")
                            } else {
                                Text("1 cup")
                            }

                        }
                    }
                    .pickerStyle(WheelPickerStyle())
                    .labelsHidden()
                }
                .font(.headline)

                Text("You chose: Cups # \(coffeeAmount)")

But the selected number of cups is never set to the coffeeAmount variable and always remains at 1.0 It's like the binding - simply just doesnt update the coffeeAmount.

I dont get it.

2      

Hi @cathalfarrell, it is because your coffeeAmount is a double. if you change by @State private var coffeeAmount = 1 you should not have this problem anymore

4      

 @State private var coffeAmmount = 1

 Section(header: Text("Daily coffe intake")
                    .font(.headline)){
                        Picker("How many cups", selection: $coffeAmmount){
                            ForEach(1..<21){
                                Text($0 > 1 ? "\($0) cups" : "\($0) cup")
                            }
                        }
                }

how to change from default "2 cups" to 1 cup. ? My coffeAmmount is 1 so as I understand $0 should also be 1 in Text view. But its showing default as 2. Can someone explain ?

thanks.!!

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.