1 00:00:00,330 --> 00:00:01,200 ‫Instructor: Hi. 2 00:00:01,200 --> 00:00:04,260 ‫Within this lecture, we are going to define the views 3 00:00:04,260 --> 00:00:06,000 ‫into our view controller 4 00:00:06,000 --> 00:00:08,280 ‫and then, we're going to work on them. 5 00:00:08,280 --> 00:00:11,790 ‫So let me add a new editor over here 6 00:00:11,790 --> 00:00:14,070 ‫and you can click on this view controller 7 00:00:14,070 --> 00:00:18,420 ‫to bring view controller on the right hand side. 8 00:00:18,420 --> 00:00:22,890 ‫Or remember, you can choose the viewController.swift 9 00:00:22,890 --> 00:00:24,270 ‫from here as well. 10 00:00:24,270 --> 00:00:26,580 ‫So, this is the same thing. 11 00:00:26,580 --> 00:00:28,080 ‫And in the left hand side, 12 00:00:28,080 --> 00:00:30,120 ‫we already see the main storyboard. 13 00:00:30,120 --> 00:00:35,010 ‫So, all we have to do is click drag and drop the view. 14 00:00:35,010 --> 00:00:39,930 ‫So, remember you have to press control in your keyboard 15 00:00:39,930 --> 00:00:43,440 ‫and then drag and drop inside of the class, 16 00:00:43,440 --> 00:00:46,200 ‫but outside of this function. 17 00:00:46,200 --> 00:00:49,490 ‫So inside of this class, I'm going to call this 18 00:00:49,490 --> 00:00:53,760 ‫"firstNumber" or "firstTextNumber", okay? 19 00:00:53,760 --> 00:00:55,740 ‫I'm going to call this "firstText." 20 00:00:55,740 --> 00:00:59,310 ‫I believe this is the simplest thing that I can call. 21 00:00:59,310 --> 00:01:00,810 ‫So this is "firstText." 22 00:01:00,810 --> 00:01:04,290 ‫Let me add some couple of more lines, 23 00:01:04,290 --> 00:01:06,600 ‫and I'm going to bring in 24 00:01:06,600 --> 00:01:08,765 ‫the "secondText." 25 00:01:08,765 --> 00:01:11,310 ‫So this is "secondText." 26 00:01:11,310 --> 00:01:12,540 ‫Okay? 27 00:01:12,540 --> 00:01:14,487 ‫So let's say, "Connect", 28 00:01:15,330 --> 00:01:19,740 ‫and then, I believe we have to bring our label 29 00:01:19,740 --> 00:01:22,290 ‫and then, our buttons as well. 30 00:01:22,290 --> 00:01:24,150 ‫Remember, if you made a mistake, 31 00:01:24,150 --> 00:01:26,760 ‫you can break the connections over here 32 00:01:26,760 --> 00:01:29,850 ‫and delete everything and redefine, 33 00:01:29,850 --> 00:01:33,303 ‫but if not, just don't go with it. 34 00:01:34,260 --> 00:01:37,200 ‫So I'm going to need some more actions 35 00:01:37,200 --> 00:01:39,330 ‫to connect to over here. 36 00:01:39,330 --> 00:01:43,080 ‫But first I'm going to get my resultLabel. 37 00:01:43,080 --> 00:01:47,940 ‫So let me bring this over here and call this "resultLabel." 38 00:01:47,940 --> 00:01:52,500 ‫Okay, so we are done with the text fields labels. 39 00:01:52,500 --> 00:01:54,810 ‫Now let's go for actions. 40 00:01:54,810 --> 00:01:59,220 ‫So first will be my adding summation function. 41 00:01:59,220 --> 00:02:00,750 ‫So let me 42 00:02:00,750 --> 00:02:04,110 ‫try to take this and bring this over here 43 00:02:04,110 --> 00:02:06,930 ‫and this is going to be an action, 44 00:02:06,930 --> 00:02:10,739 ‫and let's call this something like "someAction" 45 00:02:10,739 --> 00:02:13,020 ‫or "someButtonClicked", okay? 46 00:02:13,020 --> 00:02:14,760 ‫Let's call it "someClicked" 47 00:02:14,760 --> 00:02:17,010 ‫and connect to our view controller. 48 00:02:17,010 --> 00:02:21,480 ‫So we have our first action, let's go for others. 49 00:02:21,480 --> 00:02:25,500 ‫So, the second one will be deduction. 50 00:02:25,500 --> 00:02:28,590 ‫So I'm going to call this "minusClicked." 51 00:02:28,590 --> 00:02:30,900 ‫I'm not being consistent over here. 52 00:02:30,900 --> 00:02:34,740 ‫One is "someClicked" and the second one is "minusClicked." 53 00:02:34,740 --> 00:02:36,577 ‫Maybe I could have called it 54 00:02:36,577 --> 00:02:40,680 ‫"deductionClicked" or something, but it doesn't matter. 55 00:02:40,680 --> 00:02:44,730 ‫So third one will be "multiplyClicked." 56 00:02:44,730 --> 00:02:46,170 ‫Okay? 57 00:02:46,170 --> 00:02:50,250 ‫And we are here for the last one. 58 00:02:50,250 --> 00:02:51,210 ‫Okay? 59 00:02:51,210 --> 00:02:53,730 ‫So let's go for the last one, 60 00:02:53,730 --> 00:02:57,450 ‫and drag and drop to our view controller. 61 00:02:57,450 --> 00:03:00,540 ‫So I'm going to call this "divideClicked." 62 00:03:00,540 --> 00:03:02,343 ‫So, let's call "divideClicked" 63 00:03:03,480 --> 00:03:05,070 ‫and then connect. 64 00:03:05,070 --> 00:03:06,000 ‫So here we go. 65 00:03:06,000 --> 00:03:10,530 ‫We have defined everything in our view controller. 66 00:03:10,530 --> 00:03:13,920 ‫So, we are now ready to go and write our code. 67 00:03:13,920 --> 00:03:18,420 ‫So let me close this down and let me choose view controller 68 00:03:18,420 --> 00:03:20,760 ‫and run this on my simulator 69 00:03:20,760 --> 00:03:24,600 ‫to actually see how it looks like 70 00:03:24,600 --> 00:03:29,313 ‫on a real phone or a real simulator screen, okay? 71 00:03:30,270 --> 00:03:32,670 ‫It's wise to do that at this point 72 00:03:32,670 --> 00:03:34,890 ‫because if you see any problem 73 00:03:34,890 --> 00:03:38,070 ‫without going through all of the changes 74 00:03:38,070 --> 00:03:39,540 ‫in the coding section, 75 00:03:39,540 --> 00:03:42,690 ‫you may want to adjust the views a little bit 76 00:03:42,690 --> 00:03:44,970 ‫and then continue writing your code. 77 00:03:44,970 --> 00:03:46,470 ‫So let me 78 00:03:46,470 --> 00:03:48,510 ‫bring this simulator up 79 00:03:48,510 --> 00:03:52,773 ‫and see if we can get the result over here. 80 00:03:53,970 --> 00:03:57,120 ‫And maybe we can just take a look 81 00:03:57,120 --> 00:03:59,520 ‫at the simulator's headings as well 82 00:03:59,520 --> 00:04:03,150 ‫because we have seen how to run the simulator, 83 00:04:03,150 --> 00:04:05,550 ‫but we haven't seen the settings 84 00:04:05,550 --> 00:04:09,300 ‫of the simulator here as well because they, 85 00:04:09,300 --> 00:04:12,390 ‫simulators have their own settings, 86 00:04:12,390 --> 00:04:16,260 ‫and then you click on a simulator, you can reach their menu. 87 00:04:16,260 --> 00:04:17,190 ‫Okay? 88 00:04:17,190 --> 00:04:20,550 ‫So these are two different kind of softwares 89 00:04:20,550 --> 00:04:23,580 ‫and they have their own menus, 90 00:04:23,580 --> 00:04:27,120 ‫so you can access them from here. 91 00:04:27,120 --> 00:04:32,040 ‫So we see our app, we click on first number for example, 92 00:04:32,040 --> 00:04:34,440 ‫and we can try to write something. 93 00:04:34,440 --> 00:04:35,460 ‫Yeah, it'll write. 94 00:04:35,460 --> 00:04:38,670 ‫So everything seems to be in order. 95 00:04:38,670 --> 00:04:41,250 ‫And let me show you what I mean by settings. 96 00:04:41,250 --> 00:04:45,120 ‫If you go to debug section over here, for example, 97 00:04:45,120 --> 00:04:46,980 ‫you can see some location or, 98 00:04:46,980 --> 00:04:50,250 ‫if you go to hardware, you can see the keyboard. 99 00:04:50,250 --> 00:04:54,390 ‫So maybe you can just say, "Toggle software keyboard", 100 00:04:54,390 --> 00:04:58,020 ‫and it will pop up the keyboard for you, okay? 101 00:04:58,020 --> 00:05:01,140 ‫So you can type it from here as well. 102 00:05:01,140 --> 00:05:03,870 ‫Generally, developers don't like to use it that way, 103 00:05:03,870 --> 00:05:07,170 ‫so they can type it from their own keyboard, 104 00:05:07,170 --> 00:05:09,690 ‫but you can change the settings 105 00:05:09,690 --> 00:05:11,550 ‫from this keyboard menu as well. 106 00:05:11,550 --> 00:05:15,270 ‫So, you can do whatever you want with this simulator. 107 00:05:15,270 --> 00:05:19,440 ‫You can change its location, you can change its name. 108 00:05:19,440 --> 00:05:22,230 ‫So this is a great tool and as you can see, 109 00:05:22,230 --> 00:05:24,750 ‫nothing happens as I click on these buttons 110 00:05:24,750 --> 00:05:26,880 ‫because I haven't written them yet. 111 00:05:26,880 --> 00:05:31,050 ‫So let's stop this and go to viewController.swift, 112 00:05:31,050 --> 00:05:34,170 ‫in which where we are going to write our codes. 113 00:05:34,170 --> 00:05:37,110 ‫So we are going to start with the sumClicked. 114 00:05:37,110 --> 00:05:41,520 ‫We are going to test the first function and if that works, 115 00:05:41,520 --> 00:05:43,830 ‫we're going to reapply the same techniques 116 00:05:43,830 --> 00:05:46,110 ‫to other functions as well. 117 00:05:46,110 --> 00:05:48,750 ‫So what do I wanna do in the sumClicked? 118 00:05:48,750 --> 00:05:51,930 ‫First I want to get the first number, right, 119 00:05:51,930 --> 00:05:54,090 ‫and then I want to get the secondNumber, 120 00:05:54,090 --> 00:05:57,030 ‫and I want to display the result 121 00:05:57,030 --> 00:06:00,990 ‫of their summation together in the resultLabel. 122 00:06:00,990 --> 00:06:03,960 ‫So let me create firstNumber variable. 123 00:06:03,960 --> 00:06:07,170 ‫So the first number will be firstText, right? 124 00:06:07,170 --> 00:06:10,770 ‫So this will be the place, this will be the view 125 00:06:10,770 --> 00:06:14,670 ‫that our user is going to be writing their inputs. 126 00:06:14,670 --> 00:06:19,470 ‫So I'm going to say, "firstText.text." 127 00:06:19,470 --> 00:06:23,490 ‫So if you just look at the attributes and methods over here, 128 00:06:23,490 --> 00:06:26,490 ‫you will see something like text. 129 00:06:26,490 --> 00:06:30,210 ‫So this is the text displayed by the text field. 130 00:06:30,210 --> 00:06:35,210 ‫So whatever has our user written in this text field, 131 00:06:35,490 --> 00:06:39,480 ‫we can get it by saying, "firstText.text." 132 00:06:39,480 --> 00:06:42,360 ‫So this is cool, we already got 133 00:06:42,360 --> 00:06:45,390 ‫what is written inside of the first text, 134 00:06:45,390 --> 00:06:48,990 ‫but this is going to be an string, 135 00:06:48,990 --> 00:06:51,450 ‫it's not gonna be an integer. 136 00:06:51,450 --> 00:06:52,530 ‫Okay? 137 00:06:52,530 --> 00:06:57,030 ‫So we just got this "firstText.text", 138 00:06:57,030 --> 00:07:01,560 ‫and it doesn't make sense to add this string to another. 139 00:07:01,560 --> 00:07:03,840 ‫We have to convert this to, 140 00:07:03,840 --> 00:07:08,160 ‫into an integer or a double in order to make the same thing 141 00:07:08,160 --> 00:07:12,810 ‫with the secondText and they sum it up together. 142 00:07:12,810 --> 00:07:15,390 ‫So wrap this around an integer 143 00:07:15,390 --> 00:07:19,830 ‫and this will convert this text into an integer. 144 00:07:19,830 --> 00:07:21,150 ‫What is the problem here? 145 00:07:21,150 --> 00:07:22,920 ‫So we have 146 00:07:22,920 --> 00:07:26,910 ‫to force cast this or we have to provide 147 00:07:26,910 --> 00:07:31,080 ‫a double question mark in our in order to make this optional 148 00:07:31,080 --> 00:07:34,593 ‫and provide a default value as you might remember. 149 00:07:35,700 --> 00:07:40,170 ‫So I'm going to force unwrap this first. 150 00:07:40,170 --> 00:07:44,280 ‫I will try and see if that works with force unwrapping. 151 00:07:44,280 --> 00:07:46,350 ‫If that works, very good, 152 00:07:46,350 --> 00:07:48,540 ‫if that doesn't, I will change it. 153 00:07:48,540 --> 00:07:49,380 ‫Okay? 154 00:07:49,380 --> 00:07:52,350 ‫I'm going to do the same thing for secondNumber as well. 155 00:07:52,350 --> 00:07:54,990 ‫So I'm going to say, "Let's secondNumber 156 00:07:54,990 --> 00:07:57,840 ‫is going to be the integer 157 00:07:57,840 --> 00:08:00,750 ‫of secondText.text", okay? 158 00:08:00,750 --> 00:08:04,983 ‫Int secondText.text. 159 00:08:06,270 --> 00:08:08,100 ‫So that's okay. 160 00:08:08,100 --> 00:08:12,630 ‫And, we are going to sum this two together 161 00:08:12,630 --> 00:08:14,610 ‫and we still haven't done yet. 162 00:08:14,610 --> 00:08:17,250 ‫So we have to force unwrap this one more time. 163 00:08:17,250 --> 00:08:19,290 ‫I'm going to show you why. 164 00:08:19,290 --> 00:08:23,640 ‫I'm going to create another value over here, 165 00:08:23,640 --> 00:08:25,650 ‫I'm going to call this "result." 166 00:08:25,650 --> 00:08:28,110 ‫This is going to be the integer. 167 00:08:28,110 --> 00:08:31,320 ‫This is going to be firstNumber plus secondNumber, right? 168 00:08:31,320 --> 00:08:32,700 ‫So this is first number, 169 00:08:32,700 --> 00:08:36,213 ‫but we see, this is integer optional, 170 00:08:37,080 --> 00:08:38,700 ‫so this isn't good. 171 00:08:38,700 --> 00:08:42,990 ‫So secondNumber is integer optional as well. 172 00:08:42,990 --> 00:08:45,150 ‫And guess what, we cannot do that 173 00:08:45,150 --> 00:08:49,260 ‫because the binary operator of summation, 174 00:08:49,260 --> 00:08:52,860 ‫binary operator of summing these two numbers together 175 00:08:52,860 --> 00:08:56,013 ‫cannot be applied to integer optionals, 176 00:08:57,840 --> 00:09:00,210 ‫so we have force unwrapped this. 177 00:09:00,210 --> 00:09:03,660 ‫Why it is an optional? 178 00:09:03,660 --> 00:09:07,590 ‫Because right now, the first exclamation point 179 00:09:07,590 --> 00:09:12,590 ‫only stands for firstText.text is going to be a text, 180 00:09:13,440 --> 00:09:15,240 ‫we are certain of that. 181 00:09:15,240 --> 00:09:20,010 ‫And if we put another exclamation point over here, 182 00:09:20,010 --> 00:09:24,790 ‫it means that this is definitely going to be an integer. 183 00:09:26,130 --> 00:09:29,580 ‫So as you can see now, this doesn't give me any error, 184 00:09:29,580 --> 00:09:34,580 ‫because the first number is not an integer optional anymore. 185 00:09:35,700 --> 00:09:39,090 ‫So let me tell you one more time, 186 00:09:39,090 --> 00:09:44,050 ‫the first exclamation point means that this firstText.text 187 00:09:44,910 --> 00:09:47,220 ‫is going to be definitely over there. 188 00:09:47,220 --> 00:09:51,090 ‫And we know that even if user doesn't give an input, 189 00:09:51,090 --> 00:09:54,720 ‫we are going to get a string from there, right, 190 00:09:54,720 --> 00:09:58,290 ‫an empty string, we are not going to get a nil value, 191 00:09:58,290 --> 00:10:00,510 ‫we are certain of that. 192 00:10:00,510 --> 00:10:02,910 ‫So this is, 193 00:10:02,910 --> 00:10:06,840 ‫over here, we are certain that firstText is defined 194 00:10:06,840 --> 00:10:11,840 ‫and it's not going to return us a nil value, it's going to, 195 00:10:13,260 --> 00:10:16,983 ‫at the worse, it's going to return us an empty string. 196 00:10:18,030 --> 00:10:20,670 ‫And we are making certain of that 197 00:10:20,670 --> 00:10:24,780 ‫by putting this exclamation point in this stage. 198 00:10:24,780 --> 00:10:27,780 ‫And if we add the other one, if we add the second one, 199 00:10:27,780 --> 00:10:30,630 ‫it means that we are certain that 200 00:10:30,630 --> 00:10:35,190 ‫we are definitely going to convert it into an integer. 201 00:10:35,190 --> 00:10:37,860 ‫Let's see if we delete this, 202 00:10:37,860 --> 00:10:41,994 ‫and if I come over here and if I write firstNumber, 203 00:10:41,994 --> 00:10:45,660 ‫it will make this integer an optional integer, okay? 204 00:10:45,660 --> 00:10:49,320 ‫And this is okay for Xcode, this is optional. 205 00:10:49,320 --> 00:10:54,320 ‫But if I put this, this is going to be, and not optional, 206 00:10:54,780 --> 00:10:57,630 ‫this is going to be a regular integer. 207 00:10:57,630 --> 00:11:01,080 ‫And by this time we are betting, we are saying 208 00:11:01,080 --> 00:11:04,110 ‫that this is definitely going to be a string 209 00:11:04,110 --> 00:11:06,180 ‫and this string is definitely going 210 00:11:06,180 --> 00:11:09,693 ‫to be converted into integer. 211 00:11:10,620 --> 00:11:14,550 ‫And later on, I can just take this resultLabel 212 00:11:14,550 --> 00:11:18,030 ‫and I can say, "resultLabel.text 213 00:11:18,030 --> 00:11:20,460 ‫is now result." 214 00:11:20,460 --> 00:11:21,293 ‫Okay? 215 00:11:21,293 --> 00:11:24,750 ‫But I cannot say that because resultLabel.text 216 00:11:24,750 --> 00:11:27,360 ‫is expecting a string from me 217 00:11:27,360 --> 00:11:31,710 ‫and I'm giving an integer to it, right? 218 00:11:31,710 --> 00:11:34,350 ‫So I have to convert this into a string. 219 00:11:34,350 --> 00:11:38,490 ‫So I can just go like this, string result. 220 00:11:38,490 --> 00:11:41,340 ‫So this will convert result into a string 221 00:11:41,340 --> 00:11:44,790 ‫and I can definitely do that, once I got the result, 222 00:11:44,790 --> 00:11:49,110 ‫I can definitely make an integer into a text, right? 223 00:11:49,110 --> 00:11:52,410 ‫This is going to be okay anyway. 224 00:11:52,410 --> 00:11:57,000 ‫So let me run this and let's see if this works. 225 00:11:57,000 --> 00:11:59,790 ‫So we are gonna get the firstText. 226 00:11:59,790 --> 00:12:04,790 ‫So let me give this something like 40 or, five. 227 00:12:05,040 --> 00:12:09,330 ‫So let me give this 10, let me just sum them together, 228 00:12:09,330 --> 00:12:11,730 ‫and here we go, the 15. 229 00:12:11,730 --> 00:12:15,780 ‫So let me give minus 10 and minus five. 230 00:12:15,780 --> 00:12:18,900 ‫So let me give a big number, 231 00:12:18,900 --> 00:12:21,210 ‫and let me give a very big number. 232 00:12:21,210 --> 00:12:25,920 ‫So that's still working and it doesn't seem to be crashing, 233 00:12:25,920 --> 00:12:30,920 ‫but if I come over here and say, "Apple", okay, 234 00:12:30,990 --> 00:12:33,810 ‫and if I want to sum them together, 235 00:12:33,810 --> 00:12:36,930 ‫as you can see, it crashed. 236 00:12:36,930 --> 00:12:41,370 ‫So, this is definitely going to be this way 237 00:12:41,370 --> 00:12:43,770 ‫because as you can see it says 238 00:12:43,770 --> 00:12:46,920 ‫that it is unexpectedly found nil 239 00:12:46,920 --> 00:12:49,140 ‫while unwrapping an optional. 240 00:12:49,140 --> 00:12:51,570 ‫So we have said that this is definitely 241 00:12:51,570 --> 00:12:55,500 ‫going to be over there, but it's not there. 242 00:12:55,500 --> 00:12:59,070 ‫We couldn't even convert this to be an integer. 243 00:12:59,070 --> 00:13:02,070 ‫So since this integer is not there, 244 00:13:02,070 --> 00:13:05,760 ‫since this in integer is a nil, 245 00:13:05,760 --> 00:13:09,060 ‫the application crashed. 246 00:13:09,060 --> 00:13:10,830 ‫And the good thing is, 247 00:13:10,830 --> 00:13:14,430 ‫you know how to make this safe, right? 248 00:13:14,430 --> 00:13:18,180 ‫You know what to do in a a situation like this. 249 00:13:18,180 --> 00:13:21,570 ‫Of course you can go with double question marks 250 00:13:21,570 --> 00:13:26,520 ‫or you can go with default values, but we do not want that. 251 00:13:26,520 --> 00:13:28,080 ‫We just want 252 00:13:28,080 --> 00:13:33,080 ‫to have a safe number and have a safe calculation, 253 00:13:33,300 --> 00:13:38,190 ‫so that even if the user doesn't give the right input, 254 00:13:38,190 --> 00:13:41,160 ‫we would not crash the app 255 00:13:41,160 --> 00:13:43,920 ‫and we would get the result that we want. 256 00:13:43,920 --> 00:13:46,353 ‫We are gonna do that in the next lecture.