Your first item in the Picker is Text("Select a Handle") and you've set the tag to nil. So, if someone picks the first item then the selectedHandle is going to be nil. That cannot be stored in roundsdata.roundsHandle, because you've said it's 'text' (I'm sure you meant String).
So, just don't update roundsdata.roundsHandle in onChange when it's nil, i.e.:
.onChange(of: selectedHandle) {
if(selectedHandle != nil) {
roundsdata.roundsHandle = selectedHandle
}
}
Next, your players in the ForEach have a player value that you're setting the tag to, so when someone picks something other than the first item (which is nil) it will attempt to set roundsdata.roundsHandle to that value, but your tag is player as PlayerData?.
That means you're trying to set roundsdata.roundsHandle to an Optional(PlayerData). That's why you're getting the error.
You need to set the tag to a String value from the player, not to a PlayerData object.
You have Text(player.playerHandle) so the player.playerHandle is a String. I think that's what you need to set the tag to, i.e.:
Picker("Handle:", selection: $selectedHandle) {
Text("Select a Handle").tag(nil)
ForEach(players, id: \.self) { player in
Text(player.playerHandle)
.tag(player.playerHandle)
}
}
.onChange(of: selectedHandle) {
if(selectedHandle != nil) {
roundsdata.roundsHandle = selectedHandle
}
}
It looks like you don't completely understand how these pickers work because you're over-complicating it. This might help:
Picker("Select something", selection: $thisIsPopulatedWithTheThingSelected) {
// List the things we want in the picker, like days of the week
ForEach(myArrayOfDayStrings, id: \.self) { day in
Text(day) // Shows Monday-Sunday
.tag(day) // For Monday, the tag is Monday. For Tuesday, it's Tuesday etc.
// When someone picks Monday, $thisIsPopulatedWithTheThingSelected becomes Monday. Pick Tuesday and it's Tuesday.
}
}
.onChange(of: thisIsPopulatedWithTheThingSelected) {
print("You picked \(thisIsPopulatedWithTheThingSelected)")
}