|
I'm working on Project 2 https://www.hackingwithswift.com/books/ios-swiftui/showing-the-players-score-with-an-alert This is what Paul wrote:
My Questions is: Why
three image displayed through ForEach -> user choose the answer through flagTapped, right ? Hence, the number of the images shown, and the nubmer of user choosed , shoudn't be the same number parameters, rights ? However, the syntx make it looks like it is the same number used in this
So how could Swift know which number Thank you in advance , Boat |
|
What maybe confusing here is that the If you select the keyword Possibly the easiest way to think of the way ForEach works is think of it like this:- Quite literally, for each of the collection of things I am going to give you, give me one of them at a time and I am going to do something with it.
|
|
but still, in the code Paul wrote
ForEach has to loop 0 ,1, 2 to show three images, right ? But supposedly, Button should only run once , and that is after when all three Images have been all shown and when the user made his/her choice. NO?
|
|
Nothing is "run" when the So, in the case of What results is essentially this:
|
|
Hello Now I got the idea that Like @Obelix said, Button is "fancy listing", meaning, rather than modestly showing images, these images actually bearing functions. Otherwise, why choose Therefore, with Button bearing flagTapped function, if there's no flag tapped, the function aspect of Button won't be triggerd, and these buttons will be used merely for showing its lables (flag images in this case), which is good enough for ForEach to make views already. Right ? So back to my original question, the (number) in Thanks again everyone, Boat |
|
I think that Obelix's 'fancy listing' was just because it was an upmarket restaurant ;-) You're right that buttons do more than images - they have two parts - how they look and what they do when you tap on them. A Button conforms (meaning matches and follows the template) for the View protocol, which is what the ForEach requires and expects. So yes, you are right. In essence you are right, the flagTapped(number) action is only triggered for the corresponding Button (flag image) tapped with the appropriate number value. Maybe reading Paul's tutorials on buttons might help. |
|
Yes I just read the Button piece you provided above, very helpful. Thank you Love from Singapore, Boat (becuse of the time difference, I couldn't attend Paul's livestream yesterday, but my love for this community is growing everyday.) |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
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.