All right. 1
So now that we've learned all about structures, let's go ahead and remove our functional, but slightly 2
ugly bit of code here where we have our 2D quiz array, and instead let's turn it into an array of quiz 3
items created from a structure. In order to create a structure, 4
we're going to go ahead and make a new file. So you can either go to File, New, File... Or simply just right 5
click on one of these folders where you want the file to live inside, and go to New File. 6
And there, we're going to choose a new Swift file. 7
So under iOS, choose a Swift file and hit Next. 8
And now we're going to name our file: Question. 9
Now, it's convention that we have the same file name as the name of our structure. 10
So our structure is going to be called Question. 11
So ideally, you want your file to also be called Question. 12
Now, it's not obligatory, it's just good programming practice so that other programmers will be able to 13
recognize what it is that you're doing. 14
So inside this new File, we're going to create a struct with the struct keyword. 15
And if I hit enter, you can see that it uses the code snippet for a struct to help me create this new 16
Struct. And struct are pretty easy. 17
They just have a name and some fields. 18
So the name of my struct is going to be Question with a capital Q. So now that I've created my struct, 19
I'm going to give it some fields. So let's delete that place holder and let's go ahead and think about 20
what fields or properties would make sense to be associated with our Question. 21
Well, each question really needs a bit of question text and an answer 22
that's the right answer. 23
So in our Question struct., I'm going to create those two fields or properties and I'm gonna create them 24
using constants. 25
So we have a constant code text which is gonna be of data type string and we have a constant code answer 26
which is also going to be of data type string. 27
Now that we've created our properties, we can go ahead and initialize it inside our ViewController. 28
Right here, we're going to replace this array with a question item. To initialize a new structure, write 29
the name of the structure, and then we add a pair of parentheses. And then inside here, we're going to 30
go ahead and initialize some of those fields that we had, namely the question text and the answer. 31
So the text is, of course, this one, and the answer is going to be true. So we're going to paste that in 32
there and we can now delete our previous array. 33
So now we have a question object here and we can replace these two as well. So now we have an array of 34
question objects and that's reflected when we hold down option and we click on our quiz array. 35
You can see it's now a collection. 36
So it's square brackets. And inside the collection, 37
we've got a whole bunch of Question objects. So now we've avoided the sort of ugliness of a 2D Array. 38
And our quiz array is now far easier to understand. 39
It's just got a whole bunch of questions, each with a text and an answer. 40
So now we have to go and fix our code to use this new format. Here instead of tagging that one at the 41
end, 42
we're instead going to write .answer because this is going to look inside our quiz array and it's 43
going to pull out the item at this particular position. 44
So let's say that we put out the first one. 45
So then we get a question object and we can tap into its answer property by simply writing .answer. 46
Now, if you find it easier, you can actually split it into two parts. So you can say the actualQuestion 47
which is equal to quiz at questionNumber. 48
And this allows you to inspect on its data type which is a question data type. 49
So that's that custom data type that we created. 50
And then, of course, this question data type has two properties: text or answer. 51
So that means if we then go ahead and write actualQuestion, then we can write a dot and we can see all 52
the properties that it has, text and answer. 53
So you can either do it this way or the previous way whichever way you find easier to understand. 54
Now, I'm gonna keep it like this just to keep it short and sweet because I think this should be explanatory 55
enough. 56
So now all we have to do is to fix the last line of code inside our updateUI function. 57
I'm going to leave this to you as a challenge. 58
Try and fix this error using what we've just learned about accessing properties from our structures. 59
Pause the video now. 60
All right. 61
So again, we're going to remove this second part because we used to have a 2D Array and now we don't. 62
And instead, I'm going to tap into the text property to populate my questionLabel text property. 63
So now if we run our code, you'll notice that absolutely nothing about the functionality or the appearance 64
has changed, but our code is now structured a lot better, and we can easily extend it by creating more 65
structures in the way that we have done here. 66
And we have code that is now easily understandable at a glance here where we create our quiz array, as 67
well as here, where we tap into the quiz questions properties. 68
Now, we can actually make this code even simpler if we went into our question struct and actually created 69
a custom initializer. So we can say that when we have to initialize a new question instance, instead 70
of putting text an answer which is quite wordy, why don't we just have "q" which is going to be a string 71
and "a:" which is going to be a string. That way when we initialize our question, we can set our text to 72
equal "q" and we can set our answer to equal "a." 73
Now, when we initialize our question object here, instead of having to write text and answer, all we need 74
is just "q" and "a," and this way we can customize how we initialize our structures so that we write even 75
less code and even more succinct code. Now that we've understood how this works, we're going to go ahead 76
and actually delete all of these questions that we've created in order to learn about structures, and 77
instead, I want you to go into themREADME file and scroll down to the section where it says Default 78
Quiz. I want you to copy all the questions that are currently inside that section and then we're going 79
to paste it inside our quiz array. So to save you from typing a million lines, we've created all of these 80
questions for you in the structure of the question that we have created. 81
So now if you run the app, you'll see that we now have a working quiz with 12 question items.