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

SOLVED: Question: a simple Picker is not working

Forums > 100 Days of SwiftUI

@boat  

Here is my code. No warning in Xcode, and I could build in simulator and the Picker will show.

But after I made a choice in the picker, it couldn't return the choice to $chosenPosition1.

It is not a complicated case, but I'm just confused. Anyone could spot where I was wrong ?

I crossed compared with Paul's tutorial on Picker , still don't understand what I did wrong. https://www.hackingwithswift.com/quick-start/swiftui/how-to-create-a-picker-and-read-values-from-it

(make the array of Positions a constant or a var didn't make any differences, I tried both.)


import SwiftUI

struct Player {
    let name: String
    let nationality: String
    let gender: Bool
    let age: Int
}

let player001 = Player(name: "Djokovic", nationality: "SRB", gender: false, age: 34)

struct ContentView: View {

    let positions = ["1","2","3","4","5","6","7","8"]
    @State private var chosenPosition1 = 1

  var body: some View {
        NavigationView {
            Form {
                Section {
                    VStack (spacing: 80) {
                        HStack (spacing: 80){
                            VStack (spacing: 55){
                                Text ("\(player001.name)")

                                }
                            VStack (spacing: 50){
                                Picker("position", selection: $chosenPosition1) {
                                    ForEach (positions, id:\.self) {
                                        Text ($0)
                                    }
                                }
                            }

                       }

                }
                    Section {
                        Text ("SUBMIT")
                    }
               }
          }
      }
   }
 }

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

2      

    let positions = ["1","2","3","4","5","6","7","8"]
    @State private var chosenPosition1 = 1

Your array of values for populating the Picker should really be of the same type as the variable you use to track the current value of the Picker. Here, you have String and Int. (They can be different types, but that requires more work and is unnecessary in this case.)

So change your code to:

    let positions = 1...8
    @State private var chosenPosition1 = 1

And then your Picker to this:

Picker("position", selection: $chosenPosition1) {
    ForEach(positions, id: \.self) {
        Text("\($0)")
    }
}

3      

@boat  

Awesome ! @@roosterboy

It looks so obvious when you pointed out ! I'm so silly.

Have a great day !

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.