|
If I understand ForEach correctly when used in a Picker, the value ForEach passes to the Picker is actually the row value and not the number in the range. So if like below, I have a range of 10..<21, when the ForEach is on the value of 12, it's only passing the value of 2 to the Picker in the selection binding, because it's on row 2.
|
|
Elsewhere I may have written:
You may have confused yourself by naming the incoming variable: The ForEach must be able to process objects conforming to the When those ten Whenever you DO click on one of the |
|
To be clear, you asked:
The answer is "No". In the internal guts of a The This contrived example may help. Copy this into Playgrounds.
As a test, add the month "MARCH" (all caps) to the end of the array. I think you'll notice that the values in the array are different you'll have "March" and "MARCH" in the picker. But both of these items will have an |
|
Thanks for the clarification. 👍 So with a text range this seems to be true:
but with array of text items the selection variable holds the actual value at the index. Which seems a little confusing.
|
|
Thanks for testing my answer!Please see my revised answers for an update. It's not using the value at the array's index, it seems to be using the value supplied as the object's id within the ForEach structure. |
|
After reading your update, I tried something different with a struct that has an id field and it works just like you said. It's using the object's id. So now, I understand why when using a range or an array things appear the way they do.
Also, I didn't know you could display Pickers in the playgrounds so that's cool. Though, I had to remove this line to get your example to work in mine:
|
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
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.