|
I really like this challenge. Although, I was surprised that it took me so long to complete. I had to leave it and come back to it for many days because I was getting too frustrated with it not working for me. 2 things challenged me:
Unfortunately, try as I might I could not get this solution to work. The @State variable just wouldn't change it's value. I finally worked out that this functions properly (excerpt from my project code):
Explicitly associating the tag with the Picker entry seems to be the only way to get this to work. I am using the most recent macOS on Intel with all updates applied. Maybe something is broken with my XCode installation...
|
|
Apple's docs, at the end of the Overview section on the same page you link to, say to use a
That matching to the type of the state variable is the important thing to remember with |
|
Thanks @roosterboy; but immediately after the overview section; in the Iterating Over a Picker's Options section it describes using ForEach and says this:
I think my selection type is the same as the input to the ForEach; but if I don't use the tag() it just doesn't work. If you could spare another moment could you please let me know where I have this wrong - perhaps TemperatureUnits.allCases doesn't produce a TemperatureUnits enum type...
This is where I think the documentation/my understanding/installation, is broken; because the example looks almost identical to what I put in my code. |
|
To follow up - In a new project I did a straight copy of the documentation's code and it just doesn't work (selectedFlavor doesn't change it's value) on my system - strange.
I think there's some kind of problem with using String for the Identifiable protocol. If I set the id to the rawValue explicitly it still doesn't work:
but if I set the id to the enum itself it does work:
|
|
The key is the bolded part:
The type of the
So to get it to work without explicitly specifying a tag, you would need your
With this change, Another option to avoid having to explicitly set a
We can do without Again, the key is that the bound variable for the selection has to be the same type as the items' Edit: I do think you are right that there is an error in Apple's documentation. Or, since |
|
Thank you so much @roosterboy. I really appreciate the time you spent on this helping me. Now I understand what's happening and why. I sent Apple a bug report for the documentation page. |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.
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.