Here is my list of objects:
let gameTypes = [
GameType(id: "generalKnowledge", text: "❓ General Knowledge"),
GameType(id: "geography", text: "🌎 Geography"),
GameType(id: "music", text: "🎤 Music"),
GameType(id: "sport", text: "⚽️ Sport"),
GameType(id: "technology", text: "💻 Technology"),
GameType(id: "movies", text: "📺 Movies & TV"),
]
And my Picker in my view:
@State var gameType: GameType = gameTypes[0]
var body: some View {
NavigationView {
VStack {
// Create game
HStack {
Picker("\(gameType.text)", selection: $gameType) {
ForEach(gameTypes, id: \.id){ type in
Text("\(type.text)").tag("\(type.id)")
}
}
.pickerStyle(MenuPickerStyle())
My Picker successfully shows all gameTypes items in a list/menu when tapped. However when I tap an item in that list, gameType does not update.
Any idea why?
EDIT:
GameType
class GameType: Hashable, Equatable {
let id: String
let text: String
init(id: String, text: String) {
self.id = id
self.text = text
}
// Conform to Hashable
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
// Conform to Equatable (players will not be the same)
static func == (lhs: GameType, rhs: GameType) -> Bool {
return false
}
}
