|
I have a Picker view inside of a form... myRating is an Array of rating objects loaded from a database. Everything works great but once I select a value it doesn't show selected on the form, only blank. Any help would be aweseom, Thanks!!
|
|
|
|
SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
Just double checking a mistake I made when I made a picker. Is selectedRating an Int? When you select the top item in the picker, selectedRating would be 0 (zero), not one. |
|
No, it is a string. It doesn't matter what value I choose from the picker, it doesn't show up. |
|
From Paul's wonderful list of helpful information.
The picker wants $selectedRating to be a number, not the value of the array element (the actual rating). Paul's example has selectedColor as 0 to start and then shows the colors[0] if that one was selected in the picker. |
|
I'm sorry, yes my selectedRating is an Int... I'll try and rewrite it to use the index of the array. @State private var selectedRating = 0 |
|
|
|
OK, sorry I missed this earlier. It appears you are taking the sysArrays.myRating and not using it in order (if (rating.rate!.contains("A")). So later, when you try to access the array, the picker isn't pointing to the same spot. Below code will show you what I mean. So I would make a helper function that takes sysArrays.myRating and puts it in an order that will be displayed, and can be picked from. (Found from StackOverflow... put it anywhere outside your ContentView)
Underneath your picker, put in these 2 items (temporarily)
Unless I am mistaken, that will show the array of items you're trying to pick from
That should show the number of the array element selected. When you change the picker, the first Print should stay the same, the Text one should change each time you pick a new item on the picker. |
|
|
|
What did it show in the console for sysArrays.myRating? If it isn't the picker order that you want, then there is the issue I believe. Try creating a function that accepts sysArrays.myRating and returns a new array with the items (in order) for the picker. Then the picker will return 0 if it is the top one, increase by 1 until the last item. |
|
|
|
Obviously I don't have the data associated this this, but when I made a test app with some items, it worked. Mine (works as I think you intend)
Yours
|
|
sysArrays.myRating is an array of Rating objects. For some reason I can't call .count on the array. |
|
i dont know if this will make a difference but is your Rating object conform to Identifiable? Try that and also change the ForEach to included id: .self |
|
It didn't work for me either untill I added |
|
In your ForEach, you need to make it conform to Identifiable or add an id as I have below, and add a tag of the same type as the value you want returned. You can fix that with: ForEach(sysArrays.myRating, id:.self) { rating in if (rating.rate!.contains("A")){ Text(rating.rate ?? "Error") .bold() .font(.headline) .foregroundColor(.green) .tag(rating) |
|
Does this code do what you want it to do?
|
SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.