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