0
1
00:00:00,270 --> 00:00:06,270
In the last lesson, we started to reorganize our code after the Model-view-controller design pattern.
1

2
00:00:06,270 --> 00:00:09,430
We've just added the check and some method to our Quiz Brain.
2

3
00:00:09,960 --> 00:00:12,080
However, our code is still incomplete.
3

4
00:00:13,200 --> 00:00:20,720
So in order to make this work, we need to get our checkAnswer function to return an output and that
4

5
00:00:20,730 --> 00:00:28,260
output has to be either true or false, so that we can determine what color to tell the UI to change to.
5

6
00:00:29,670 --> 00:00:36,960
If we head back over to our QuizBrain, let's modify our checkAnswer function to allow for an output and
6

7
00:00:36,960 --> 00:00:39,590
the data type is going to be a boolean.
7

8
00:00:39,630 --> 00:00:42,960
It's either the user got it right or the user got it wrong.
8

9
00:00:42,960 --> 00:00:44,750
True or false.
9

10
00:00:44,790 --> 00:00:50,950
Now, we're getting an error saying that we're missing a return in a function expected to return 'Bool.'
10

11
00:00:51,180 --> 00:00:56,430
We said that we would return an output, but nowhere in our code do we actually do that.
11

12
00:00:57,210 --> 00:01:05,770
Let's go ahead and delete these comments and return true if the user got it right, and return false
12

13
00:01:05,850 --> 00:01:07,910
if the user got it wrong.
13

14
00:01:08,340 --> 00:01:14,690
Now that our function has an output, we need to catch that value at the point where we call this function,
14

15
00:01:15,090 --> 00:01:18,110
namely in our ViewController.swift.
15

16
00:01:18,120 --> 00:01:25,110
Here is where we call our function that belongs to the QuizBrain. And when this line runs, then this
16

17
00:01:25,110 --> 00:01:28,660
line will be replaced by the value of the output.
17

18
00:01:28,710 --> 00:01:32,270
So let's go ahead and capture it inside a constant.
18

19
00:01:32,340 --> 00:01:34,770
So I'm going to call this constant
19

20
00:01:34,770 --> 00:01:36,810
userGotItRight
20

21
00:01:37,050 --> 00:01:41,280
and I'm gonna set it to equal the output of this function call.
21

22
00:01:41,280 --> 00:01:44,340
So if userGotItRight is equal to true, it means they got it right.
22

23
00:01:44,340 --> 00:01:50,550
If userGotItRight is false, it means they got it wrong. And now, instead of checking this whether if
23

24
00:01:50,550 --> 00:01:55,870
user answer is equal to actual answer, we can check if userGotItRight.
24

25
00:01:55,920 --> 00:02:00,330
Now, because this is already going to be a true or false value,
25

26
00:02:00,480 --> 00:02:06,830
we don't actually have to make any checks to see if it's equal to true or equal to false.
26

27
00:02:06,930 --> 00:02:14,730
As long as the value is a true or false a boolean, then we can simply make our check like so. 
27

28
00:02:14,730 --> 00:02:23,250
You can, of course, go much further to shorten this code. But always remember to optimize for readability, and readability
28

29
00:02:23,250 --> 00:02:28,440
depends on your level of skill or the people you're working with.
29

30
00:02:28,440 --> 00:02:33,860
If another programmer was looking at your code, would they be able to easily understand?
30

31
00:02:33,930 --> 00:02:40,980
For example, if I was to shorten this to quizBrain.checkAnswer (sender.currentTitle!)
31

32
00:02:41,160 --> 00:02:44,690
Maybe, maybe not.
32

33
00:02:44,750 --> 00:02:46,650
I'll let you be the judge of that.
33

34
00:02:46,670 --> 00:02:55,640
This part of the code does exactly the same as all of this code, but we've just split it out into several
34

35
00:02:55,640 --> 00:02:57,870
components to make it easier to understand
35

36
00:02:57,890 --> 00:02:58,690
step by step.
36

37
00:03:00,320 --> 00:03:04,330
Next, let's turn our attention to the updateUI function.
37

38
00:03:04,370 --> 00:03:09,950
Here we're grabbing the question tags to display and we're also calculating the user's progress.
38

39
00:03:10,430 --> 00:03:14,170
However, these two things sound like the perfect job for our model,
39

40
00:03:14,180 --> 00:03:20,240
don't you think? As a challenge, I'd like you to use your knowledge of functions to move this logic into
40

41
00:03:20,240 --> 00:03:20,920
the QuizBrain.
41

42
00:03:21,590 --> 00:03:29,810
I want you to fix both of these lines of code 48 and 51 or whatever line your code is on, so that you
42

43
00:03:29,810 --> 00:03:34,720
end up with a View Controller that has code that looks like this,
43

44
00:03:34,730 --> 00:03:47,810
quizBrain.GetQuestion text. And down here, it should be something like quiz Brain.getProgress.
44

45
00:03:48,740 --> 00:03:55,820
And you need to be able to fix the quizBrain to make these two lines of code work. At the moment,
45

46
00:03:55,820 --> 00:04:02,960
they won't because there isn't such a thing as getProgress or getQuestionText. But if you go ahead
46

47
00:04:02,960 --> 00:04:08,450
and modify your View Controller to make it look exactly like this, and then complete the challenge by
47

48
00:04:08,510 --> 00:04:13,490
adding the necessary code to the quizBrain. Pause the video now and give that a go.
48

49
00:04:17,030 --> 00:04:17,440
All right.
49

50
00:04:17,470 --> 00:04:22,300
So we know that we need to grab the question text with this method,
50

51
00:04:22,310 --> 00:04:25,280
getQuestionText inside our quizBrain.
51

52
00:04:25,340 --> 00:04:31,880
So let's go ahead and create this method. Inside our quizBrain, maybe just below our checkAnswer,
52

53
00:04:31,880 --> 00:04:33,830
I'm going to create that new function
53

54
00:04:33,830 --> 00:04:40,050
getQuestionText, and then I'm going to make sure that this function has an output.
54

55
00:04:40,220 --> 00:04:43,420
And the question text is, of course, a string data type.
55

56
00:04:43,490 --> 00:04:52,850
So that's going to be my output. And what I'm going to return is the question text which corresponds
56

57
00:04:52,850 --> 00:04:55,000
to the current questionNumber.
57

58
00:04:55,070 --> 00:05:01,160
So I'm going to use my return keyword, and then I'm going to search through my quiz for the one at the
58

59
00:05:01,160 --> 00:05:09,930
position of my questionNumber, and then I'm going to get the text property of that question.
59

60
00:05:10,200 --> 00:05:17,130
So now if we hit save and we go back, and you might need to beat your code with a stick by hitting command
60

61
00:05:17,130 --> 00:05:19,680
B for it to refresh and update.
61

62
00:05:19,740 --> 00:05:26,580
But this line is now completely error free and all the bits of code are colored in the right colors.
62

63
00:05:26,580 --> 00:05:28,350
Now, let's fix the next one.
63

64
00:05:28,350 --> 00:05:36,390
So we need a method called getProgress in our quizBrain that returns a floating point number that
64

65
00:05:36,390 --> 00:05:38,480
we can assign as the progress.
65

66
00:05:38,520 --> 00:05:44,450
So if we go back into our quizBrain and create that function called getProgress.
66

67
00:05:44,550 --> 00:05:51,040
And this, of course, has to return a float because that's what the progress view requires.
67

68
00:05:51,060 --> 00:05:57,120
Now, in order to work out the progress, we can, of course, use the equation that we used before which is
68

69
00:05:57,150 --> 00:06:04,440
the current questionNumber divided by the total number of items inside our quiz array,
69

70
00:06:04,560 --> 00:06:12,030
so quiz.count. And then because these are integers, we have to first convert them into floating point
70

71
00:06:12,030 --> 00:06:15,640
numbers before we make that division.
71

72
00:06:15,780 --> 00:06:19,910
And now we can return our progress.
72

73
00:06:20,580 --> 00:06:26,850
Now, if we hit save and we go back to our view controller and hit command B to build, then you can see
73

74
00:06:26,940 --> 00:06:29,220
this line is now passing as well.
74

75
00:06:29,550 --> 00:06:31,770
So well done if you manage to get that right.
75

76
00:06:31,830 --> 00:06:32,720
If you didn't,
76

77
00:06:32,790 --> 00:06:41,070
be sure to review the last lesson on functions with outputs to get a better idea of how it works. The
77

78
00:06:41,070 --> 00:06:48,420
next thing we're going to do is let's deal with the quiz progression functionality. The part that keeps
78

79
00:06:48,510 --> 00:06:56,120
track of the questionNumber should really be inside the quizBrain because it's kind of a quiz functionality.
79

80
00:06:56,250 --> 00:07:04,030
Let's go ahead and cut it out from here and move it over to our quizBrain. In our quizBrain,
80

81
00:07:04,050 --> 00:07:12,330
I'm going to create a new function called nextQuestion which is going to handle the question progression
81

82
00:07:12,330 --> 00:07:19,230
functionality. And then I'm going to paste the code that I had from before into here and I can command
82

83
00:07:19,260 --> 00:07:29,310
"a" to select and go to Structure and Re-indent to need to up with the code, so that it's easier to see. Now that
83

84
00:07:29,310 --> 00:07:33,390
we've removed these lines of code from our view controller,
84

85
00:07:33,390 --> 00:07:34,970
we have to replace it.
85

86
00:07:34,970 --> 00:07:41,340
So at the position where we used to have it, we have to replace it with a call to quizBrain.nextQuestion,
86

87
00:07:41,340 --> 00:07:47,190
so that we pass the baton on over to here. At this point,
87

88
00:07:47,250 --> 00:07:54,450
we've restructured our code quite a bit and we're slowly starting to conform to the MVC design pattern.
88

89
00:07:54,510 --> 00:07:59,700
You can now see how our model is getting all the quiz related responsibilities.
89

90
00:07:59,760 --> 00:08:03,200
It's now our Quiz Brain that knows all about the quiz questions.
90

91
00:08:03,240 --> 00:08:05,280
None of you control them.
91

92
00:08:05,280 --> 00:08:08,870
It's also our Quiz Brain that decides if the user got the answer right,
92

93
00:08:08,880 --> 00:08:10,130
which question comes next,
93

94
00:08:10,230 --> 00:08:14,530
and how far the user has progressed. But notice how
94

95
00:08:14,600 --> 00:08:17,390
we've now got some different errors over here
95

96
00:08:17,390 --> 00:08:24,790
and it says something about left side of mutating operator isn't mutable" 'self' is immutable.
96

97
00:08:24,830 --> 00:08:31,910
So what exactly is immutable and what does it mean? If you already know all about this, then feel free
97

98
00:08:31,910 --> 00:08:37,130
to skip the next Deep Dive. But otherwise, head over to the next lesson where we're going to do a Deep
98

99
00:08:37,130 --> 00:08:41,570
Dive on immutability and I'll see you on whichever lesson you choose.
