0
1
00:00:00,630 --> 00:00:07,200
In the last lesson, we added the logic to progress to the next question into our Quiz Brin. However,
1

2
00:00:07,200 --> 00:00:12,360
Xcode gave us an error as soon as we tried to change the questionNumber, and the reason is we're trying
2

3
00:00:12,360 --> 00:00:16,590
to change a property of our struct from its methods.
3

4
00:00:16,590 --> 00:00:19,340
Here is our struct blueprint.
4

5
00:00:19,770 --> 00:00:26,940
And here when we actually use that struct, that quizBrain, well, this is the actual struct object.
5

6
00:00:26,940 --> 00:00:31,090
So each of these properties, say, the questionNumber or the quiz,
6

7
00:00:31,160 --> 00:00:37,710
well, they are associated with that object created from the struct, and they cannot be changed.
7

8
00:00:37,860 --> 00:00:43,470
The only way we can change them from inside one of our methods is to mark those methods with the keyword
8

9
00:00:43,560 --> 00:00:44,950
mutating.
9

10
00:00:45,000 --> 00:00:48,160
In other words, we have two kinds of methods in our structure.
10

11
00:00:48,210 --> 00:00:55,010
The first kind is a method that doesn't change anything and just returns a value like getProgress.
11

12
00:00:55,110 --> 00:01:00,090
And the second method is one that changes a property in the Quiz Brain.
12

13
00:01:00,090 --> 00:01:07,020
The only thing we have to do is to help identify which functions will change a property in the
13

14
00:01:07,020 --> 00:01:07,660
Quiz Brain.
14

15
00:01:07,680 --> 00:01:15,030
That's why we have to mark our next question method as mutable. So if we click on this suggestion, you
15

16
00:01:15,030 --> 00:01:23,100
can see that it's telling us one of the fixes is to mark this method as mutating to make 'self' mutable.
16

17
00:01:23,220 --> 00:01:28,980
So if we click Fix, you can see it as the mutating keyword before the func keyword.
17

18
00:01:28,980 --> 00:01:34,620
And what this tells the computer is that when this function is run, it will replace the old value of
18

19
00:01:34,620 --> 00:01:38,010
questionNumber with a new value.
19

20
00:01:38,010 --> 00:01:45,840
So every time you create a struct, and inside one of the functions or one of its capabilities, you need
20

21
00:01:45,840 --> 00:01:48,490
to be able to update one of the properties,
21

22
00:01:48,600 --> 00:01:51,200
so a var or changeable property,
22

23
00:01:51,300 --> 00:02:00,380
you have to do that inside a mutating function. So if we run our app right now, you can see there are
23

24
00:02:00,380 --> 00:02:04,850
app works perfectly just as it did before.
24

25
00:02:04,850 --> 00:02:12,560
But in this case, we've shifted all of the quiz logic into a separate structure called the QuizBrain
25

26
00:02:13,280 --> 00:02:21,260
and our controller or the ViewController now is only responsible for delegating these tasks to the
26

27
00:02:21,260 --> 00:02:23,170
quizBrains, say,"Hey, quizBrain,
27

28
00:02:23,170 --> 00:02:30,410
give me the next question," or "Hey, quizBrain, check this answer for me," or "Get me the questionText,"
28

29
00:02:30,410 --> 00:02:37,940
and the control is now talking to our model to be able to make the quiz work. And our controller is also
29

30
00:02:37,940 --> 00:02:45,320
talking to our user interface, namely changing the button background colors or updating the UI every
30

31
00:02:45,320 --> 00:02:52,550
time we go to the next question. And the code is now much easier to understand, more reusable and more
31

32
00:02:52,550 --> 00:02:53,360
modular.
32

33
00:02:53,510 --> 00:03:02,180
So we've now implemented the model-view-controller design pattern to our Quizzler App. To practice coding
33

34
00:03:02,180 --> 00:03:04,440
in this MVC design pattern,
34

35
00:03:04,580 --> 00:03:10,730
we're going to set you a challenge. And the challenge is to make our app be able to keep track of the
35

36
00:03:10,730 --> 00:03:12,700
user's score.
36

37
00:03:13,070 --> 00:03:19,310
So what you're gonna do is you're gonna create a new label and you're going to drag it to the very top
37

38
00:03:19,430 --> 00:03:27,650
of our vertical stack view, so you get to place it here. And then, we're going to modify some of the attributes
38

39
00:03:27,650 --> 00:03:30,500
or properties of this label.
39

40
00:03:30,500 --> 00:03:38,690
And I'm going to change the text color to white to be able to see it against the dark background.
40

41
00:03:38,780 --> 00:03:46,940
Now, the next thing I want to do is I'm going to create an IBOutlet that links from this label to my
41

42
00:03:47,030 --> 00:03:48,340
view controller.
42

43
00:03:48,440 --> 00:03:56,780
So I'm going to create it just about maybe here and I'll call it a scoreLabel.
43

44
00:03:56,810 --> 00:04:04,400
Now, finally, we're gonna go to our view controller. And under updateUI, I'm going to set the scoreLabel's text
44

45
00:04:04,460 --> 00:04:09,730
property to equal to, say, "Score:"
45

46
00:04:09,740 --> 00:04:17,870
And then I'm going to use my string interpolation to insert a method to get from my quizBrain.
46

47
00:04:17,880 --> 00:04:22,810
So I'm gonna tap into my quizBrain and I'm gonna say getScore.
47

48
00:04:22,820 --> 00:04:27,130
So here comes the challenge for you. At the moment,
48

49
00:04:27,200 --> 00:04:29,810
there is no such thing as getScore in quizBrain.
49

50
00:04:29,810 --> 00:04:38,360
So pause the video and make sure that all of your score tracking and score managing and score keeping
50

51
00:04:38,360 --> 00:04:41,900
functionality happens inside the QuizBrain.
51

52
00:04:41,900 --> 00:04:47,240
And if you manage to successfully complete this challenge, this error should, of course, go away.
52

53
00:04:47,240 --> 00:04:51,140
But, also, we should be able to see the score start out with zero.
53

54
00:04:51,440 --> 00:04:55,730
And every time we get an answer right, the score will go up by 1.
54

55
00:04:55,760 --> 00:05:02,460
So if we get an answer wrong, nothing changes, but all the correct answers will increase our score.
55

56
00:05:02,690 --> 00:05:10,760
And once we get to the end of our quiz and it resets, well, our score should also reset to zero.
56

57
00:05:10,820 --> 00:05:18,020
So this is the challenge for you to practice using MVC and making sure that this quiz-related functionality
57

58
00:05:18,050 --> 00:05:20,160
lives inside the QuizBrain.
58

59
00:05:20,330 --> 00:05:23,950
Pause the video now and try to complete this challenge.
59

60
00:05:27,320 --> 00:05:27,610
All right.
60

61
00:05:27,640 --> 00:05:29,820
So here comes the solution.
61

62
00:05:29,860 --> 00:05:35,560
We know that we can't write any code in our view controller which is solely responsible for updating
62

63
00:05:35,560 --> 00:05:38,380
the UI and talking to our model.
63

64
00:05:38,380 --> 00:05:44,770
So we have to make our model be able to perform the functionality of keeping track of score and sending
64

65
00:05:44,770 --> 00:05:47,690
it back via this method called getScore.
65

66
00:05:48,100 --> 00:05:53,190
So if we go over to the QuizBrain, this is where all of the heavy lifting is going to happen.
66

67
00:05:53,230 --> 00:05:57,730
So the first thing I'm going to do is I'm going to create a variable that's going to keep track of the
67

68
00:05:57,730 --> 00:06:03,220
score in a similar way that I'm keeping track of the questionNumber that the user is on.
68

69
00:06:03,280 --> 00:06:06,280
The score is going to start out with a value of zero.
69

70
00:06:06,430 --> 00:06:08,800
But when is it going to get increased?
70

71
00:06:08,800 --> 00:06:14,080
Well, whenever we check the user's answer and it matches the correct answer,
71

72
00:06:14,080 --> 00:06:19,810
well, in that case, not only do we return true and tell our view controller that the user got it right,
72

73
00:06:20,170 --> 00:06:24,880
but we also increase the value of score by 1.
73

74
00:06:24,880 --> 00:06:28,840
Now, we're not going to do anything to punish them when they get it wrong.
74

75
00:06:28,960 --> 00:06:36,040
But we do have to address the issue of the mutating function because, again, we're modifying one of the
75

76
00:06:36,040 --> 00:06:38,230
properties of a struct
76

77
00:06:38,350 --> 00:06:46,690
so, therefore, we have to mark this function as mutating, so that we rebuild our structure with a new value
77

78
00:06:46,690 --> 00:06:48,600
for score.
78

79
00:06:48,650 --> 00:06:55,730
Now, the next thing we need to do is to be able to send this score back through the function that the
79

80
00:06:55,730 --> 00:06:58,450
view controller cause which is called getScore.
80

81
00:06:58,670 --> 00:07:05,330
So I'm going to copy that. And in my QuizBrain just above my getQuestionText, I'm going to create a
81

82
00:07:05,330 --> 00:07:12,110
new function called getScore and this is going to return the score which is, of course, a integer or
82

83
00:07:12,110 --> 00:07:13,550
a whole number.
83

84
00:07:13,550 --> 00:07:20,880
And it's simply going to return the value of our score property which lives inside our Quiz Brain.
84

85
00:07:21,620 --> 00:07:29,690
So now the very last thing we need to do with score is to remember to reset it down to zero when our
85

86
00:07:29,690 --> 00:07:31,010
quiz restarts.
86

87
00:07:31,010 --> 00:07:35,860
So not only do we reset our questionNumber, we also reset our score.
87

88
00:07:36,020 --> 00:07:42,730
And now if I run my app, firstly, my errors should go away because this method now exists, and also I'm
88

89
00:07:42,740 --> 00:07:47,300
now able to keep track of the user's score. when they get a question right,
89

90
00:07:47,360 --> 00:07:53,380
the score goes up, and when they get to the end of the quiz, the score resets.
90

91
00:07:53,480 --> 00:07:55,590
So did you manage to get that right?
91

92
00:07:55,910 --> 00:08:03,740
If you want more practice on using the MVC design pattern to be able to switch out or modify just the
92

93
00:08:03,740 --> 00:08:07,730
Quiz Brain component and leave everything else as is,
93

94
00:08:07,820 --> 00:08:15,830
we've got an optional challenge for you. And the challenge here is if you go into the README, you can
94

95
00:08:15,830 --> 00:08:21,990
see that in addition to the default quiz, we've also got a section called the Multiple Choice Quiz.
95

96
00:08:22,100 --> 00:08:30,200
So if you were to copy all of these multiple-choice questions and replace the questions that's currently
96

97
00:08:30,200 --> 00:08:37,820
inside our quiz array with the multiple-choice questions, can you figure out how to modify the code in
97

98
00:08:37,820 --> 00:08:42,320
the app so that you end up with a multiple choice quiz?
98

99
00:08:42,320 --> 00:08:48,860
And if you manage to successfully complete the challenge, then this is what your app will look like. And
99

100
00:08:48,860 --> 00:08:55,940
it's now modified to be able to serve up three possible answers, and then the user can select the answer
100

101
00:08:56,420 --> 00:08:57,970
as one from the three.
101

102
00:08:58,010 --> 00:09:03,030
So there's a number of things that you'll have to modify in the app for this to be able to work.
102

103
00:09:03,230 --> 00:09:09,520
And if you want to tackle this optional challenge, then go ahead and replace the quiz array now.
103

104
00:09:09,710 --> 00:09:16,760
And once you're done, you can compare your answer, against my answer by downloading the completed project
104

105
00:09:16,820 --> 00:09:22,310
for the Multiple Choice Quiz which we'll be able to find, of course, in the course resources.
105

106
00:09:22,310 --> 00:09:26,900
But either way, no matter what you choose, I'll see you on the next module.
