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