NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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      

Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try 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.