|
In project 2: part 2, Paul delves into the logic for checking whether the correct flag has been tapped, but in all honesty this feels a bit like black magic to me for a couple reasons:
My question most revolves around how the flag's text is able to properly display itself in the ForEach loop. How does the code know to display both the text and flag for Germany, for example? And why does that method above claim to answer my question (when in reality it doesn't because I'm still left wondering how two different pieces of code can do the same thing, if that makes sense). TLDR: I want to know how the code knows how to display both the correct country text and its corresponding flag. |
|
Hi! When you
When this var is intialized, it assigns random number, let's assume this number is 2 The code runs down the body and sees that part
As it is a loop it will run 3 times and each time assigning the corrsponding digit In the Image part it will do as follows -> As you have created these properties containing array
the
i.e.
thus retrieving correspoding images from the assets and showing them on the screen. Let's go to the As each button assigned with number 0, 1, 2 in each loop, by pressing it, it will pass that number and compare that number to your Which is created by loop as button 3, i.e. Germany Flag. You have question that was displayed as:
And
So when you pressing button with number 2 and function checks that number is says ok number 2 == number 2. You got it right!
Correc answer is posted as question:
Your buttons are displayed as:
so if there is a match between |
|
Hi pill0bokeh, the countries array is shuffled after every round and the logic is that the first three flags are shown in every round. By a random number between 0 and 2 the country is selected which flag the user shall guess. The number (between 0 and 2) is stored in correctAnswer. Lets say in the first round the 3 flags are the flags of ["Estonia", "France", "Germany"] and the correctAnswer is randomly selected as 2. Then the question is
The flags are taken from the Images which have the exact same name as the countries, so the flags Estonia.jpg, France,jpg and Germany.jpg are shown as images for the button. If you tap a button, its number (index in the array) is compared to correctAnswer and the answer is shown :-) |
|
TLDR:
Pull your big problem apart and look at smaller pieces of the problem. You have an
Each level of the game is only played with the first three cards which are numbered 0, 1, and 2. It would be a boring game if Estonia, France, and Germany were played over and over again. So at some point in the lesson you added the
Still, the game is only played with the first three entries which are still numbered, 0, 1 and 2. ForEach, the View FactoryWhen I first learned the 100 Days of SwiftUI lessons, I was confused until I thought of the
Making a ButtonIf it's easier to visualise, draw three boxes on a piece of paper, each representing ONE What I see, is the factory needs an The factory also generates three function calls, one for each After the factory finishes making these three Here's the cool thing about SwiftUI and In your Keep CodingSee -> View Factory |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your 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.