In the last lesson, we managed to create a single array which contained three questions and we were able 1
to show it on screen and show each of the questions. 2
Now, in this lesson, we're going to try and figure out how to be able to check to see if they got the 3
answer correct, 4
so whether if they chose the right answer to each question. And in the process, we'll also learn how to 5
work with a two dimensional array in Swift. 6
Now, in order to do this, we need to somehow associate an answer with each question, right? 7
We need to be able to say that, well, maybe this one should be true and this one should be True, 8
but this one should be False. 9
So how can we associate two pieces of data together even though our quiz lives inside an array? 10
Well, one way of doing this might be through the use of an array within an array or what you might call 11
a 2D Array. 12
So far, the arrays that we've been creating look something like this. 13
We have as many items as we need or separated by a comma and placed inside a single array. 14
So this is our collection of items. 15
Now, we can also create a 2D Array where we have individual arrays. So there's three arrays, in this 16
case, each with two items all packaged inside a larger array. 17
So how would you retrieve a single entry out of a two-dimensional array? When we had a one-dimensional 18
array, retrieving items was pretty straightforward. In order to retrieve items out of our array, 19
we simply added a set of square brackets and a number after the name of the array. And that number, 20
in this case, "i," refers to the position of the item that we want. Now, if we had a 2D Array on the other 21
hand, in addition to adding a first set of square brackets where we're picking through the outer layer 22
of arrays. 23
Now, that we've picked out the item that is at this position, let's say, this one which is position 2, well, 24
now we have access to yet another array. 25
And in order to pick from this inner array, we have to tag on another set of square brackets in order 26
to pick between these two items in the inner array. 27
So here's a question. If we had an array that was a 2D Array that looks like this, 28
so we have 1, 2, 3 inside its own array, 4, 5, 6 in its own array, 7, 8, 9, and then these three 29
arrays are packaged into another array. 30
Well, how do you get hold of the number 9? 31
Pause the video, 32
have a think about that. 33
What would your code look like? 34
Well, in this case, the positions of the outer array are 0, 1, and 2, and then the positions inside the array 35
are also at 0, 1, and 2. 36
So in order to get hold of this number 9, we first have to tap into our array, and then specify that 37
we want the array that at position 2. 38
So it's this one, 7, 8, 9. 39
And then within this array of 7, 8, 9, we then specify the position of the item we want which is, again, 40
at 0, 1, 2. 41
So now we have array square brackets 2, square brackets 2 in order to get that number 9 out of this 42
2D Array. Coming back to all code here, 43
we currently have a 1D Array, and we can turn it into a 2D Array by simply making each of the items 44
a array itself. 45
So we now have a question and a answer, and I'm keeping it simple by making the array keep the same type 46
of data. 47
So they're both gonna be strings, right? 48
So let's go ahead and modify the rest of these. So we now have three arrays within an array. And if we 49
take a look at the data type of our quiz constant, you can see it's now a 2D Array of strings. So it's 50
got two brackets around each side. 51
And now if we wanted to pick out the first question out of our quiz array, then instead of simply specifying 52
quiz[question number,] we also have to tag on a zero at the end because now it's going to pull out the 53
first array using the question number which starts out being equal to zero, and then within that array, 54
we want to get the first item, 55
so the one at position 0. 56
So this would be all code overall. So now that we have the quiz in a 2D Array, 57
well, let's check the answer that we have in our 2D Array against the answer that the user chose. 58
So, firstly, we have to figure out what answer they chose. 59
So right here, I'm going to create a new constant called userAnswer and I'm gonna set it to equal the 60
sender.currentTitle. So whichever title they chose from the two buttons are linked to my IBAction, 61
so True or False. 62
Now, this userAnswer is going to be equal to the string true or the string false. 63
So we can now use it to check against the actual answer. 64
Now, the actual answer is going to be equal to the quiz array at position 1, 65
right? 66
So similar to how we got our quizQuestion, 67
we're going to set this equal to the quiz array and then the first layer is going to be questionNumber. 68
So it's going to be one of these arrays depending on which question we are on. 69
And then the second one is going to be position 1 because that's where the answers live. They're all at position 70
one inside the array. 71
So now in order to check to see if the user got the answer right, all we have to do is create an if statement 72
and check to see if the userAnswer is equal to, so double equals, to the actual answer, 73
well, then in that case, that means they got it right, 74
right? 75
So we'll just print "Right!." 76
Otherwise, namely "else," well, they probably got it wrong because their answer doesn't match the actual 77
answer, 78
so we'll right wrong. 79
Now, if we run our app, we should be able to see that question checking mechanism in action. 80
So if we run our app, you can see that if I press Four plus Two is equal to Six, 81
yeah, that sounds right, 82
So let's press True. 83
We got it right. 84
Five-Tthree is greater than One, 85
And then finally, Three + Eight is less than Ten. 86
That's definitely False. 87
So now we get our last answer, but then our app crashes because we, again, fall out of the range of our 88
quiz array. 89
We don't have a fourth question. 90
So we've got a working quiz app now, although it's a pretty buggy one. 91
And your app crashes on a user's phone, 92
they won't know what's happened, 93
they won't know that the index is out of range. 94
They'll just think that you've made a terrible app. 95
So let's fix this and help you maintain your reputation as an app developer. 96
So the first thing that we need is a safety check. 97
So inside the answer button pressed, 98
before we increase the questionNumber, let's first check to see if we're actually at the end of the quiz. 99
And I'd like to pose this as a challenge to you. 100
Can you use an "if" statement and only increase the questionNumber as long as we're not yet at the end 101
of the quiz. 102
I'll give you a few seconds to pause the video before I show you my solution. 103
Okay. 104
Here's my sample solution. 105
So in our case, we want to prevent the questionNumber from being increased passed number 2. You might think 106
that you can simply write the following: If questionNumber is less than 3, then increased 107
questionNumber by 1, or you might have written it like this, if questionNumber is less than quiz.count, 108
so the length of our array, then increase questionNumber by 1. 109
But let's double-check this by running our app again. Even though we think we might have fixed the problem, 110
you'll notice something quite peculiar. 111
So if I press on the third button, my app still crashes, 112
what's going on? 113
Well, the line that's being highlighted is right here. 114
So this is, obviously, the line that's crashing my app. 115
And the reason is because the index is out of range. 116
So the only thing that has an index that changes is my questionNumber. 117
And we can see down here that the questionNumber was, again, allowed to go to 3, even though we said 118
don't increase the questionNumber, unless the questionNumber is less than the quiz count. 119
So what's happening here? 120
Well, here's what's happening. When the questionNumber is equal to 2, 2 is less than quiz.count which 121
which is 3, 122
well, now in this case, questionNumber increases by 1. 123
So questionNumber is now 3. And then it goes into the updateUI and it tries to update the UI by getting 124
the item at position 3 out of our quiz which is what crashes our app. 125
So instead of simply checking if the quizNumber is less than quiz.count, we actually have to increase 126
this by 1. 127
So now when it gets to questionNumber 2, then it's going to add 1 to it, 128
so we never go beyond 2. 129
And now if we run our app again, 130
you'll see that we've solved our bug and it no longer progresses beyond the last question. 131
Here's another challenge for you. 132
See if you can change the code to make the quiz restart from the beginning when it reaches the end, 133
so you should end up with a never-ending quiz, and it'll look something like this. 134
So here we've got our first question, 135
our second question, 136
third question, 137
back to the first question. So pause the video and try and see if you can get this functionality working 138
in your app 139
All right. 140
So how do we achieve this? 141
Well, the place where we reach the end of the quiz is, of course, when the questionNumber plus 1 is 142
actually equal to the quiz count. 143
So how do we catch that scenario? 144
Well, we can simply add an "else" statement. 145
So if it's gone beyond the number of items we have in our quiz, 146
well, then we're simply going to set the questionNumber back down to zero. 147
So the next time we update our UI, we fetch the first question out of our quiz again, and that means our 148
quiz will loop on and on endlessly at least until the user gets bored. At this point, 149
we're showing new quiz questions. 150
We're checking the user's answer and our app is no longer crashing. 151
So now is a good time to restructure and reorganize our code. 152
Currently, one of the biggest eyesores in our code is this 2D Array. 153
It's hard to read. 154
It's hard to understand, not just for you, for any programmer. So how can we make this better? 155
Well, we could use a Swift structure to create a question structure that contains a title property, 156
so the title of the question, and then a answer property, 157
so the answer of each question. 158
So if you know all about Swift structures and you can create a structure to capture our quiz array so 159
that we get rid of our 2D Array, and instead have an array of quiz items each with a title property 160
and an answer property, then you can probably skip the next Swift Deep Dive. 161
But if you've never heard of structures, then I recommend heading over to the next lesson 162
where we're gonna do a Deep Dive on that, and more. So the choice is yours and I'll see you on the next 163
lesson you choose.