1 00:00:00,650 --> 00:00:05,150 So I think that homework was actually a little bit tough if you need to code. 2 00:00:05,900 --> 00:00:10,250 The reason it's tough is because there's something you perhaps don't realize you need. 3 00:00:10,730 --> 00:00:13,400 I suspect most of you started in the following way. 4 00:00:13,790 --> 00:00:22,070 If the view doctor background color is equal to you, eye color dot white. 5 00:00:23,280 --> 00:00:25,170 Then we're going to turn it red. 6 00:00:26,450 --> 00:00:31,850 OK, that sounds pretty straightforward, and they were going to say otherwise else. 7 00:00:32,790 --> 00:00:35,190 We're going to turn the view to background color. 8 00:00:36,350 --> 00:00:38,660 To you, eye color dot white. 9 00:00:39,830 --> 00:00:46,190 That seems very simple and straightforward, I'll explain why I don't think it's a great solution in 10 00:00:46,190 --> 00:00:49,670 a minute, but let us run it on the simulator just to check it all works. 11 00:00:49,940 --> 00:00:50,570 So read. 12 00:00:50,960 --> 00:00:51,920 Read nothing. 13 00:00:51,920 --> 00:00:52,340 Nothing. 14 00:00:52,490 --> 00:00:54,350 Oh, I think I have some bad logic. 15 00:00:55,300 --> 00:01:01,480 So if the view background color is white, well, we kind of know it's white, right? 16 00:01:01,840 --> 00:01:02,960 Why isn't it flipping? 17 00:01:03,730 --> 00:01:08,050 Well, this view, if we just click it and over the top, right. 18 00:01:08,050 --> 00:01:10,900 We go through its properties, where is it now? 19 00:01:10,930 --> 00:01:11,520 There it is. 20 00:01:12,340 --> 00:01:15,070 Which is this little Hamberger icon. 21 00:01:15,070 --> 00:01:18,040 It's called, again, terrible contrast Xcode. 22 00:01:19,510 --> 00:01:23,390 We have a tent, we have a background, which is the system, background, color. 23 00:01:23,890 --> 00:01:27,080 Now, this background color may not be white, right? 24 00:01:27,320 --> 00:01:28,510 It might be slightly off white. 25 00:01:28,930 --> 00:01:35,530 And so in our logic, if that white doesn't match, then we're in trouble, aren't we? 26 00:01:37,060 --> 00:01:40,600 So what we should do is have. 27 00:01:42,510 --> 00:01:44,790 I variable up here for. 28 00:01:45,830 --> 00:01:46,680 You, Carla. 29 00:01:47,740 --> 00:01:50,110 Flipped equal to false. 30 00:01:51,170 --> 00:01:53,900 OK, now if. 31 00:01:56,700 --> 00:01:58,200 The view color flipped. 32 00:01:59,520 --> 00:02:03,290 Is not true, so we want to put an exclamation in it. 33 00:02:05,810 --> 00:02:07,220 Then we're going to flip to red. 34 00:02:08,310 --> 00:02:15,750 Otherwise, we're going to flip to white now, there's something very, very subtle here that new programmers 35 00:02:15,750 --> 00:02:20,010 won't realize by creating an extra line of code. 36 00:02:20,460 --> 00:02:21,410 Buechler flipped. 37 00:02:21,600 --> 00:02:28,110 You think I've created more work for myself, but you haven't because I can change this to anything 38 00:02:28,110 --> 00:02:30,690 I like and this to anything I like. 39 00:02:30,990 --> 00:02:38,490 And the whole system still works before when I use the if you background color, if I change something 40 00:02:38,490 --> 00:02:43,500 below, I would then have a second place to change it, which is the initial color of the view. 41 00:02:43,770 --> 00:02:45,910 And we don't actually want to do that, do we. 42 00:02:46,500 --> 00:02:50,130 We don't want to have two things to change spread throughout our code. 43 00:02:50,580 --> 00:02:53,730 And yes, it looks easy now because they were right next to each other. 44 00:02:53,730 --> 00:02:54,830 So we knew what to do. 45 00:02:55,440 --> 00:03:00,420 But in reality, a real application, a small app has thousands of lines of code. 46 00:03:01,140 --> 00:03:04,430 So changing things across multiple files is painful. 47 00:03:05,250 --> 00:03:06,300 So let's hit play on that. 48 00:03:09,370 --> 00:03:14,800 And now, no matter what initial color it is, when I click that button, we get red and when I click 49 00:03:14,800 --> 00:03:17,570 it again, oh dear, we get nothing. 50 00:03:17,980 --> 00:03:18,640 Do you know why? 51 00:03:19,120 --> 00:03:23,770 Because I need to flip this right. 52 00:03:24,220 --> 00:03:26,810 I need a way to change this to true or false. 53 00:03:27,250 --> 00:03:34,360 So if I come below the if else and say view color flipped is equal to not view color flipped. 54 00:03:35,460 --> 00:03:39,270 So this little note here is called a unary operator. 55 00:03:39,300 --> 00:03:41,700 It does something to one variable. 56 00:03:42,120 --> 00:03:46,980 And what I'm doing is changing this in the first instance from false to the opposite, which is the 57 00:03:46,980 --> 00:03:48,740 exclamation, which is true. 58 00:03:50,130 --> 00:03:53,700 Again, an error many people make is to take this. 59 00:03:56,060 --> 00:03:57,530 And drop it in here like that. 60 00:03:58,650 --> 00:04:02,690 And like that, and again, two places where we have to change code. 61 00:04:03,090 --> 00:04:04,640 Have you heard of the dry principle? 62 00:04:04,650 --> 00:04:06,530 Do not repeat yourself. 63 00:04:07,470 --> 00:04:09,850 That is classic code mantra. 64 00:04:09,870 --> 00:04:15,990 You need to do that to have clean applications, the opposite of dry as wet, which means write everything 65 00:04:15,990 --> 00:04:18,620 twice, which I've just done here to prove my point. 66 00:04:19,080 --> 00:04:22,830 So I'm going to hit command said a couple of times just to get it back. 67 00:04:23,670 --> 00:04:27,810 Now, when we press play, my logic is in place. 68 00:04:30,680 --> 00:04:32,060 So we get red, white. 69 00:04:33,290 --> 00:04:35,000 And I think you get the idea. 70 00:04:35,970 --> 00:04:40,860 Now, the final thing we're going to do just whilst we're here is click this button in the interface 71 00:04:40,860 --> 00:04:41,370 builder. 72 00:04:43,070 --> 00:04:51,800 And we are going to change the text of it, so where is our text up what we have in this array of properties? 73 00:04:52,370 --> 00:04:57,650 So it's the third icon from the right hand side at the top of this list that you need to click. 74 00:04:58,100 --> 00:05:02,120 And if you scroll up and down, eventually you'll come to. 75 00:05:03,240 --> 00:05:11,230 The little text box right there, which allows you to type something such as toggle torch. 76 00:05:11,610 --> 00:05:13,620 Now, when I press enter, look what happens. 77 00:05:13,650 --> 00:05:14,830 It gets smashed together. 78 00:05:15,330 --> 00:05:22,380 So if you double click this little or single click this little horizontal bar that constrains the width 79 00:05:22,380 --> 00:05:29,610 of our button air, it is if you double click it, you get an interface, build a thing right on the 80 00:05:29,610 --> 00:05:30,710 button, which is very handy. 81 00:05:31,350 --> 00:05:33,630 Let's make this 100 screen points wide. 82 00:05:35,260 --> 00:05:36,820 And now we can Pressplay. 83 00:05:40,110 --> 00:05:46,830 And of course, in the simulator, we will get a beautiful screen torch app that's ready to publish. 84 00:05:48,980 --> 00:05:57,980 How simple was that so, so simple and you know why it's simple, because we understand what we do in 85 00:05:57,980 --> 00:06:05,690 the user interface, the code and all the other code files, we are putting the code in appropriate 86 00:06:05,690 --> 00:06:11,690 places so that the iOS system can do its job a lot easier. 87 00:06:12,020 --> 00:06:12,310 Right. 88 00:06:12,330 --> 00:06:16,790 So we put all of this control stuff in The View controller and nothing else. 89 00:06:17,100 --> 00:06:20,030 And we're using that to control the main storyboard. 90 00:06:20,630 --> 00:06:21,800 Simple, simple stuff. 91 00:06:22,040 --> 00:06:29,600 But you've just learned the basics for making every single app that you've ever touched in your whole 92 00:06:29,600 --> 00:06:31,310 life or ever will.