I am struggling to get pickers work in a form. I add NavigationStack and get this error Fatal error: 'try!' expression unexpectedly raised an error: SwiftUI.AnyNavigationPath.Error.comparisonTypeMismatch
I have tried most every format or picker setup as well as DatePicker and can’t seem to determine how to get the selected item in a picker list saved.
running: Xcode 16.1
import SwiftUI import SwiftData
struct RoundsEditView: View { @Bindable var roundsdata: RoundsData @Environment(.modelContext) private var modelContext // @Environment(.dismiss) var dismiss
@State private var playDate = Date.now
@State private var selectedTee = "Gold"
let tees = ["Black", "Blue", "White", "Gold", "Red", "Silver"]
@Query(sort: \PlayerData.playerHandle) private var players: [PlayerData]
@State private var selectedHandle: PlayerData? = nil
var body: some View {
NavigationStack {
Form {
HStack {
Text("Course:")
TextField("Course Name", text: $roundsdata.roundscourseName)
.textContentType(.name)
}
HStack {
// DatePicker("Date:", selection: $playDate, in: ...Date(),
DatePicker("Date:", selection: $playDate,
displayedComponents: [.date])
// DatePicker("Date:", selection: $playDate, in: ...Date(),
// displayedComponents: .date).onChange(of: playDate) { oldState, newState in model.youDidChangeMethod(from: oldState, to: newState)
}
Section {
Picker("Handle:", selection: $selectedHandle) {
Text("Select a Handle").tag(nil as PlayerData?)
ForEach(players, id: \.self) { player in
HStack {
Text(player.playerHandle)
.frame(maxWidth: .infinity, alignment: .leading)
.tag(player as PlayerData?)
}
.frame(maxWidth: .infinity, alignment: .leading)
.tag(player as PlayerData?)
}
}
// .pickerStyle(.inline) this does not fix issue or design wise work
}
// HStack {
// Text("Tee:")
// TextField("Tee", text: $roundsdata.roundsTee)
// .textContentType(.name)
// }
HStack {
Picker("Tee:", selection: $selectedTee) {
ForEach(tees, id: \.self) {
Text($0)
}
}
}
HStack {
Text("Handicap:")
TextField("Handicap", value: $roundsdata.roundsHandicap, format: .number)
.textContentType(.name)
}
HStack {
Text("*****:")
TextField("*****", value: $roundsdata.roundsGross, format: .number)
}
HStack {
Text("Net:")
TextField("Net", value: $roundsdata.roundsNet, format: .number)
.textContentType(.name)
}
HStack {
Text("Rating:")
TextField("Rating", value: $roundsdata.roundsRating, format: .number)
.textContentType(.name)
}
HStack {
Text("Slope:")
TextField("Slope", value: $roundsdata.roundsSlope, format: .number)
.textContentType(.name)
}
}
.navigationTitle("Edit Rounds")
}
}
}