For other reviewers with a similar issue. I resolved it partially from here and finally from Stackoverflow forum to a few small changes.
here is the code that works for me I changed prior response as follows:
changed:
@State private var selectedHandle: PlayerData? = nil
to
@State private var selectedHandle: String? = "Select Handle"
HStack {
Text("Handle:")
Text(roundsdata.roundsHandle)
Picker("", selection: $selectedHandle) {
Text("Select a Handle").tag(" ")
// changed tag(nil) to tag(“ “)
ForEach(players, id: \.self) { player in
Text(player.playerHandle)
.tag(player.playerHandle)
}
}
onChange(of: selectedHandle) {
if(selectedHandle != “ ") {
roundsdata.roundsHandle = selectedHandle!
// changed != nil to != “ “ both work but given all is String I went with != “ “ if someone can say why to use != nil let me know
// changed selectedHandle to selectedHandle!
}
}
}
Post
Replies
Boosts
Views
Activity
Did you get this resolved? I have the same error.
Apple developer indicates it had been deprecated For iOS versions 17.0
https://developer.apple.com/documentation/swiftui/view/onchange(of:perform:)
in any case I still get errors with the picker code. I will search for the answer.
thank you for your input.
Thank you, much appreciated.
However, that code gives me two errors.
Generic parameter 'V' could not be inferred
Cannot assign value of type 'PlayerData?' to type 'String'
Part of my issue is I have found so many variations of Pickers people have built that I have altered the pickers too many times trying to resolve. I was using hackingwithswiftui quite a bit but much has changed since those postings. I see that .onChange has been deprecated come iOS 17 so i expect much of what I am trying to do now may break in the future.
But rather than take more of your time I will review the last info your shared for me to review and see if I can figure this out.
Picker("Handle:", selection: $selectedHandle) {
Text("Select a Handle").tag(nil)
//Generic parameter 'V' could not be inferred
ForEach(players, id: \.self) { player in
Text(player.playerHandle)
.tag(player.playerHandle)
}
}
onChange(of: selectedHandle) {
if(selectedHandle != nil) {
roundsdata.roundsHandle = selectedHandle
// Cannot assign value of type 'PlayerData?' to type 'String'
}
}
Thank you. I was able to get the DatePicker work. I do get an error on the other picker using data from another view to populate the sort.
The data model field roundsHandle is defined as text.
here is the code and error
@Query(sort: \PlayerData.playerHandle) private var players: [PlayerData]
@State private var selectedHandle: PlayerData? = nil
var body: some View {
Form {
Picker("Handle:", selection: $selectedHandle) {
Text("Select a Handle").tag(nil as PlayerData?)
ForEach(players, id: \.self) {
player in Text(player.playerHandle)
.tag(player as PlayerData?)
}
}
.onChange(of: selectedHandle) {
roundsdata.roundsHandle = selectedHandle
}
The error message is on the .onchange
Cannot assign value of type 'PlayerData?' to type 'String'
so i thought it because I set PlayerData? to nil, I changed that to “ “ and get an error doing that.
ok lets see if this abbreviated code helps.
I removed the HStacks and as expected it altered the form view for the user from what we wanted so I will retain the HStacks as I do know how they work and impact the look.
my form has about 20 fields. When i don’t use Pickers I am able to add, list and update all the fields for multiple records added.
when I add the pickers, I am able to make selections but I can figure out how to get that selection to load into the field.
The date picker selection should end up in $roundsdata.roundsDate
the Handle picker is built from data created in another view and the pick list loads properly and I can select form it. However as in date how do i get the selection into $roundsdata.roundsHandle
I can’t find any examples of pickers anywhere that show how to save the selected value into a field stored in SwiftData. If you have an example like that it would be great.
This is my first attempt at creating an app so apologies for incorrect terms and sloppy code.
At the moment I have several views working as long as no pickers are involved.
import SwiftUI
import SwiftData
struct RoundsEditView: View {
@Bindable var roundsdata: RoundsData
@Environment(.modelContext) private var modelContext
@State private var playDate = Date.now
@State private var selection: selectedTee = .White
@Query(sort: \PlayerData.playerHandle) private var players: [PlayerData]
@State private var selectedHandle: PlayerData? = nil
var body: some View {
Form {
HStack {
Text("Course:")
TextField("Course Name", text: $roundsdata.roundscourseName)
.textContentType(.name)
}
DatePicker("Date:", selection: $playDate,
displayedComponents: [.date])
Picker("Handle:", selection: $selectedHandle) {
Text("Select a Handle").tag(nil as PlayerData?)
ForEach(players, id: \.self) {
player in Text(player.playerHandle)
.frame(maxWidth: .infinity, alignment: .leading)
.tag(player as PlayerData?)
}
}
I will try to clean out some of the commented text. I have it in there as reference for me as I trying to resolve the issue and not replicate something already tried. But I will take it out as best I can. I included the NavigationStack as I found that in similar responses to other Picker posts. Others said it worked buy did not for me.
As for the multiple HStacks is a formatting thing I am using on the form.
Other than the pickers everything works perfectly for me.
My issue is getting the selection from the Date Picker and other pickers loaded into my textfields for Date, Handle, Tee and others I want to create.
You said I have multiple brackets on one line. I don’t see that in my code at all so I am not sure what you mean.
I will repost with comments.
Maybe I should have added that the two fields expected to be populated are:
$roundsdata.roundsDate with the selected date and
$roundsdata.roundsHandle with the selected Handle