NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

SOLVED: Question: a simple Picker is not working

Forums > 100 Days of SwiftUI

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()
    }
}

1      

    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)")
    }
}

2      

Awesome ! @@roosterboy

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

Have a great day !

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

Get Started

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

Reply to this topic…

You need to create an account or log in to reply.

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.