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