So I think that homework was actually a little bit tough if you need to code.
The reason it's tough is because there's something you perhaps don't realize you need.
I suspect most of you started in the following way.
If the view doctor background color is equal to you, eye color dot white.
Then we're going to turn it red.
OK, that sounds pretty straightforward, and they were going to say otherwise else.
We're going to turn the view to background color.
To you, eye color dot white.
That seems very simple and straightforward, I'll explain why I don't think it's a great solution in
a minute, but let us run it on the simulator just to check it all works.
So read.
Read nothing.
Nothing.
Oh, I think I have some bad logic.
So if the view background color is white, well, we kind of know it's white, right?
Why isn't it flipping?
Well, this view, if we just click it and over the top, right.
We go through its properties, where is it now?
There it is.
Which is this little Hamberger icon.
It's called, again, terrible contrast Xcode.
We have a tent, we have a background, which is the system, background, color.
Now, this background color may not be white, right?
It might be slightly off white.
And so in our logic, if that white doesn't match, then we're in trouble, aren't we?
So what we should do is have.
I variable up here for.
You, Carla.
Flipped equal to false.
OK, now if.
The view color flipped.
Is not true, so we want to put an exclamation in it.
Then we're going to flip to red.
Otherwise, we're going to flip to white now, there's something very, very subtle here that new programmers
won't realize by creating an extra line of code.
Buechler flipped.
You think I've created more work for myself, but you haven't because I can change this to anything
I like and this to anything I like.
And the whole system still works before when I use the if you background color, if I change something
below, I would then have a second place to change it, which is the initial color of the view.
And we don't actually want to do that, do we.
We don't want to have two things to change spread throughout our code.
And yes, it looks easy now because they were right next to each other.
So we knew what to do.
But in reality, a real application, a small app has thousands of lines of code.
So changing things across multiple files is painful.
So let's hit play on that.
And now, no matter what initial color it is, when I click that button, we get red and when I click
it again, oh dear, we get nothing.
Do you know why?
Because I need to flip this right.
I need a way to change this to true or false.
So if I come below the if else and say view color flipped is equal to not view color flipped.
So this little note here is called a unary operator.
It does something to one variable.
And what I'm doing is changing this in the first instance from false to the opposite, which is the
exclamation, which is true.
Again, an error many people make is to take this.
And drop it in here like that.
And like that, and again, two places where we have to change code.
Have you heard of the dry principle?
Do not repeat yourself.
That is classic code mantra.
You need to do that to have clean applications, the opposite of dry as wet, which means write everything
twice, which I've just done here to prove my point.
So I'm going to hit command said a couple of times just to get it back.
Now, when we press play, my logic is in place.
So we get red, white.
And I think you get the idea.
Now, the final thing we're going to do just whilst we're here is click this button in the interface
builder.
And we are going to change the text of it, so where is our text up what we have in this array of properties?
So it's the third icon from the right hand side at the top of this list that you need to click.
And if you scroll up and down, eventually you'll come to.
The little text box right there, which allows you to type something such as toggle torch.
Now, when I press enter, look what happens.
It gets smashed together.
So if you double click this little or single click this little horizontal bar that constrains the width
of our button air, it is if you double click it, you get an interface, build a thing right on the
button, which is very handy.
Let's make this 100 screen points wide.
And now we can Pressplay.
And of course, in the simulator, we will get a beautiful screen torch app that's ready to publish.
How simple was that so, so simple and you know why it's simple, because we understand what we do in
the user interface, the code and all the other code files, we are putting the code in appropriate
places so that the iOS system can do its job a lot easier.
Right.
So we put all of this control stuff in The View controller and nothing else.
And we're using that to control the main storyboard.
Simple, simple stuff.
But you've just learned the basics for making every single app that you've ever touched in your whole
life or ever will.