WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

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

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      

@boat  

Awesome ! @@roosterboy

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

Have a great day !

1      

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.